我动态创建了RadioGroup
和RadioButton
,如下所示:
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioBtn2.setChecked(true);
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
在这里,步骤radioBtn2.setChecked(true);
使radioBtn2
始终处于选中状态。这意味着我不能通过选中其他两个单选按钮(radioBtn1
、radioBtn3
)来取消选中radioBtn2
。我想让RadioGroup
一次只能选中一个单选按钮(现在它可以一次选中两个单选按钮)。
我该如何解决这个问题?
发布于 2012-02-07 19:47:40
您应该像这样选中radiogroup中的单选按钮:
radiogroup.check(IdOfYourButton)
当然,您首先必须为您的单选按钮设置一个Id
编辑:我忘了,radioButton.getId()
也能用,谢谢拉梅什
EDIT2:
android:checkedButton="@+id/my_radiobtn"
在radiogroup xml中工作
发布于 2013-08-25 17:48:37
对于xml属性,它的android:checkedButton
接受要检查的RadioButton
的id
。
<RadioGroup
...
...
android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
发布于 2014-02-18 09:08:19
在XML文件中,使用默认RadioButton
的id设置RadioGroup
中的android:checkedButton
字段:
<RadioGroup
....
android:checkedButton="@+id/button_1">
<RadioButton
android:id="@+id/button_1"
...../>
<RadioButton
android:id="@+id/button_2"
...../>
<RadioButton
android:id="@+id/button_3"
...../>
</RadioGroup>
https://stackoverflow.com/questions/9175635
复制相似问题