按钮检查CheckBoxes的状态

时间:2016-05-09 02:47:50

标签: java android button checkbox

我想让我的按钮(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));
            }
        });
    }
}

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分:0)

试试这个

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();
            }
            }
        });
    }
}