我有两个复选框。如果选中一个,我希望取消选中另一个。
代码:
CheckBox ozCheckBox = (CheckBox)findViewById(R.id.oz);
boolean ozInput = ozCheckBox.isChecked();
CheckBox gCheckBox = (CheckBox)findViewById(R.id.g);
boolean gInput = gCheckBox.isChecked();
最佳答案
为什么不使用单选按钮和单选组来实现此目的?
这可以使用 RadioGroup
中的两个 RadioButton
来完成。您还可以将 RadioButton
的样式设置为与 CheckBox
完全相同。
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/two">
<RadioButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="?android:attr/listChoiceIndicatorMultiple"
android:text="CheckBox One" />
<RadioButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="?android:attr/listChoiceIndicatorMultiple"
android:text="CheckBox Two" />
</RadioGroup>
关于java - Android - 如何使选中一个复选框取消选中另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34699919/