java单选按钮设置默认值,如何在单选按钮组中将单选按钮设置为默认值?

I have created RadioGroup and RadioButton dynamically as following:

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);

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?

解决方案

you should check the radiobutton in the radiogroup like this:

radiogroup.check(IdOfYourButton)

Of course you first have to set an Id to your radiobuttons

EDIT: i forgot, radioButton.getId() works as well, thx Ramesh

EDIT2:

android:checkedButton="@+id/my_radiobtn"

works in radiogroup xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值