如何取消选中除一个框之外的所有其他复选框?

时间:2017-12-23 07:45:12

标签: android

我在警告对话框中有两个复选框。我想要的是,如果单击其中一个复选框,则会自动取消选中其他复选框。一次只能单击一个复选框。我已经在网上搜索并尝试了多种解决方案,但没有解决方案正在运行。

2 个答案:

答案 0 :(得分:0)

如果您真的想使用CheckBox,则必须以编程方式取消选中其他按钮:

private void createCustomDialogCheckBox() {

    LinearLayout dialogLayout = new LinearLayout(this);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);

    final CheckBox checkBoxOption1 = new CheckBox(this);
    final CheckBox checkBoxOption2 = new CheckBox(this);

    checkBoxOption1.setText("Option 1");
    checkBoxOption2.setText("Option 2");

    checkBoxOption1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox otherCheckBox = checkBoxOption2;
            if(isChecked && otherCheckBox.isChecked()){
                otherCheckBox.setChecked(!otherCheckBox.isChecked());
            }
        }
    });

    checkBoxOption2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox otherCheckBox = checkBoxOption1;
            if(isChecked && otherCheckBox.isChecked()){
                otherCheckBox.setChecked(!otherCheckBox.isChecked());
            }
        }
    });

    dialogLayout.addView(checkBoxOption1);
    dialogLayout.addView(checkBoxOption2);
    dialogBuilder.setView(dialogLayout);
    dialogBuilder.show();
}

更好的解决方案是使用RadioButton自动完成所有这些:

private void createCustomDialogRadioButton() {

    LinearLayout dialogLayout = new LinearLayout(this);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);

    RadioButton radioButtonOption1 = new RadioButton(this);
    RadioButton radioButtonOption2 = new RadioButton(this);

    radioButtonOption1.setText("Option 1");
    radioButtonOption2.setText("Option 2");

    RadioGroup radioGroup = new RadioGroup(this);

    // radio buttons must be on the same radio group
    // so that other buttons will automatically be unchecked / unselected

    radioGroup.addView(radioButtonOption1);
    radioGroup.addView(radioButtonOption2);

    dialogLayout.addView(radioGroup);

    dialogBuilder.setView(dialogLayout);
    dialogBuilder.show();
}

答案 1 :(得分:0)

find ids of your check boxs...
checkBoxOne= (CheckBox)findViewById(R.id.checkBox1);
checkBoxTwo= (CheckBox)findViewById(R.id.checkBox2);

checkBoxOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           Here you unchecked the other check boxes   
            checkBoxTwo.setChecked(false);
        }
    }
});

checkBoxTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           Here you unchecked the other check boxes
            checkBoxOne.setChecked(false);
        }
    }
});