RadioGroup中RadioButton默认选中问题

当一个RadioGroup(其实只要在同一个父布局)中有若干个RadioButton时,RadioButton之间存在着互斥关系,

也就是说只能选中一个RadioButton。但是如果我们需要默认选中某个RadioButton该如何处理呢? 

很简单,我们一般情况下会觉得很简单,假如我们需要设置第一个位置的radiobutton默认选中,直接会写到

	for (int i = 0; i < 3; i++) {   
 	RadioButton radioButton = new RadioButton(this);
		if(i == 0){
			radioButton.setChecked(true); 
		}     
	}

然后我们运行发现,没有问题,默认选中了。就这么简单..但是你点一下其他的就会发现,wtf...点击其他的,这个默认选中的不会被取消掉啊....

解决办法:

就是new radiobutton 的时候  给radiobutton设置一个id,如下:


for (int i = 0; i < 8; i++) {
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText(i + "");
    radioButton.setId(i);
    radioGroup.addView(radioButton, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
//默认选中第一个按钮
radioGroup.check(0);

然后运行,发现完美解决。

倘若业务需要修改,查看 ,删除的各种业务逻辑的情况的时候  参考我下面的代码设置

for (int j = 0; j < dicBeans.size(); j++) {
					RadioButton radioButton = new RadioButton(context);
					LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					radioButton.setText(dicBeans.get(j).getDicName());
					radioButton.setTag(formDetail.getTableDetailID());
					radioButton.setId(j);
					rgTypeEight.addView(radioButton, params);
					if (mIntentFrom == FLAG_SHOW ) {//查看
						radioButton.setEnabled(false);
						if(dicBeans.get(j).getDicName().equals(formDetail.getuValue())){
							rgTypeEight.check(j);
						}
					} else if(mIntentFrom == FLAG_UPDATE ){//修改
						radioButton.setEnabled(true);
						if(dicBeans.get(j).getDicName().equals(formDetail.getuValue())){
							rgTypeEight.check(j);
						}
					} else {//添加
						rgTypeEight.check(0);
						radioButton.setEnabled(true);
					}
				}

如果还有其他问题,请加我的qq群:开发一群:454430053 开发二群:537532956 开发一群:454430053 开发二群:537532956 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再见孙悟空_

你的鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值