Android 以编程方式选中的单选按钮赢得';不要取消选中
我在放射组中设置了这些单选按钮:Android 以编程方式选中的单选按钮赢得';不要取消选中,android,radio-button,radio-group,Android,Radio Button,Radio Group,我在放射组中设置了这些单选按钮: <RadioGroup android:id="@+id/radioGroupLeakTight" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton
<RadioGroup
android:id="@+id/radioGroupLeakTight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:text="@string/action_yes"
android:id="@+id/radioLeakTightYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:text="@string/action_no"
android:id="@+id/radioLeakTightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
如果我不使用上面的代码,单选按钮就可以正常工作:只要一个被选中,另一个就被取消选中。如果我使用代码预先检查其中一个单选按钮,此功能将丢失。每当我试图检查一个单选按钮时,另一个按钮仍处于选中状态
我读过一些解决方案,其中我应该实现“OnCheckedChangeListener
”,并自己切换它们。但我想这只是在掩盖其他不起作用的东西,不是吗?我也读过有这些问题的人,但他们缺少放射组,我显然有
我做错了什么?唯一的解决方案真的实现了“OnCheckedChangeListener
”并自己切换其他单选按钮吗
更新日期2016年12月8日:
所以我继续搜索并实现了“OnCheckedChanged
”。我现在看到的是,每当我检查其中一个单选按钮时,该事件从未触发。侦听器设置在两个组上。
我开始怀疑这些单选按钮失去了放射组的成员资格?每当我通过代码检查它们时(在设置侦听器之后),事件都会成功触发。使用clearCheck()
进行射线组检查。
如果您需要检查另一个单选按钮,您可以在此之后进行检查
每当我试图检查一个单选按钮时,另一个按钮保持选中状态
使用clearCheck表明我应该实现侦听器。这真的是唯一的方法吗?编辑会让你明白我的意思。这适用吗?对不起,也许我的问题不够清楚。我知道如何通过代码清除和切换单选按钮。但在我通过编程检查其中一个之后,当用户尝试使用自动切换功能时,它就会丢失。这两个值都将被检查。是的,对。因此,您应该使用侦听器。您可以
clearCheck()
并在OnCheckedChangeListener
中启用另一个单选按钮。
radioGroupLeakTight.check((currentTask.isLeakTight() ? R.id.radioLeakTightYes : R.id.radioLeakTightNo));
RadioButton radioLeakTightYes = (RadioButton) findViewById(...);
RadioButton radioLeakTightNo = (RadioButton) findViewById(...);
radioGroupLeakTight.clearCheck();
if(currentTask.isLeakTight()){
radioGroupLeakTight.check(radioLeakTightYes.getId());
} else {
radioGroupLeakTight.check(radioLeakTightNo.getId());
}