问题描述
我已经创建 RadioGroup中
和单选
动态如下
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(真);
引起 radioBtn2
必检项目。这意味着我无法取消 radioBtn2
通过检查其他两个单选按钮( radioBtn1
, radioBtn3
)。我想作出这样的 RadioGroup中
可以一次(现在,它可以检查两个单选一次)检查只有一个单选按钮。我该如何解决这个问题呢?
Here step radioBtn2.setChecked(true);
causes radioBtn2
always checked. That means I cannot uncheck radioBtn2
by checking other two radio buttons (radioBtn1
,radioBtn3
). I want to make that RadioGroup
can check only one radio button at a time (Now it can check two radiobutton at a time). How can I solve this problem?
推荐答案
您应该检查单选按钮在这样的RadioGroup中:
you should check the radiobutton in the radiogroup like this:
radiogroup.check(IdOfYourButton)
当然,你首先需要一个ID为您的单选按钮
Of course you first have to set an Id to your radiobuttons
编辑:我忘了, radioButton.getId()
作品以及,THX拉梅什
edit: i forgot, radioButton.getId()
works as well, thx Ramesh
这篇关于如何设置单选按钮与Android选中为默认RadioGroup中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!