【问题标题】:Have Button Check the State of CheckBoxes [closed]有按钮检查复选框的状态[关闭]
【发布时间】:2016-09-03 13:51:50
【问题描述】:

我想让我的按钮 (bConnect) 检查我的许可和授权复选框的状态。

如果确实选中了复选框,则允许我使用 If / Else 语句打开一个新活动,否则打开一个警告对话框并告诉用户他们没有被授权,除非这些复选框被选中。

public class ConnectAPRS extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_aprs);

        final EditText  callsign = (EditText)   findViewById(R.id.callsign);
        final EditText  passcode = (EditText)   findViewById(R.id.passcode);
        final CheckBox  licensed = (CheckBox)   findViewById(R.id.licensed);
        final CheckBox  authorized = (CheckBox) findViewById(R.id.authorized);

        final Button    bConnect = (Button)     findViewById(R.id.bConnect);

        bConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ConnectAPRS.this, Contacts.class));
            }
        });
    }
}

【问题讨论】:

  • if(checkbox.isChecked()){//打开Activity}else{//显示错误}
  • 你的复选框在哪里?
  • 似乎无法使用 CheckBox.isChecked,抛出一个我无法引用它的错误,因为它是从静态上下文中引用的
  • 在 onCreate() 方法上声明你的复选框
  • 不要使用CheckBox.isChecked() 使用licensed.isChecked()authorized.isChecked()

标签: java android button checkbox


【解决方案1】:
public class ConnectAPRS extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_aprs);

        final EditText  callsign = (EditText)   findViewById(R.id.callsign);
        final EditText  passcode = (EditText)   findViewById(R.id.passcode);
        final CheckBox  licensed = (CheckBox)   findViewById(R.id.licensed);

        final Button    bConnect = (Button)     findViewById(R.id.bConnect);

        bConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (licensed.isChecked())
                    startActivity(new Intent(ConnectAPRS.this, Contacts.class));
                else
                    new AlertDialog.Builder(ConnectAPRS.this)
                            .setTitle("ERROR")
                            .setMessage("You are not a licensed operator")
                            .show();
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    试试这个

    bConnect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                if (licensed.isChecked() && authorized.isChecked()){}
    
                    startActivity(new Intent(ConnectAPRS.this, Contacts.class));
                }else{
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("ERROR");
                    builder.setMessage("You are not a licensed operator");
                    builder.setCancelable(true);
    
                    builder.setNeutralButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
    
                    AlertDialog alert11 = builder1.create();
                    alert11.show();
                }
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 2013-09-20
      • 2017-03-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2010-09-25
      相关资源
      最近更新 更多