动态添加RadioGroup组件的RadioButton出现多选状况解决

动态添加RadioGroup组件的RadioButton出现多选状况解决
(1)使用radiogroup组件时,每个radiobutton必须有一个id,否则会出现多选,不管是xml文件还是动态添加;
(2)每一个radiobutton最好不要嵌套任何父布局,父布局直接为radiogroup即可;
(3)确保radiobutton的id不会重复,如果是在xml中,则可以直接报错,而在动态添加时,如果radiobutton的id是一样的,不会报错,但会出现多选的状况。

for (int i = 0; i < selectableGoodsPropertyList.size(); i++) {

                TextView tv_name = new TextView(ProductDetails.this);
                RadioGroup group = new RadioGroup(ProductDetails.this);
                group.setOrientation(LinearLayout.HORIZONTAL);
                group.setId(i);

                groupName = selectableGoodsPropertyList.get(i).propertyName;
                tv_name.setText(groupName);
                propertyValueList = selectableGoodsPropertyList.get(i).goodsPropertyValueList;
                for (int j = 0; j < propertyValueList.size(); j++) {
                    RadioButton btn = new RadioButton(ProductDetails.this);
                    btn.setText(propertyValueList.get(j).propertyValue);
                    btn.setId(i*(j+20)+30+j);
                    group.addView(btn);
                }
                ll_type_properties.addView(tv_name);
                ll_type_properties.addView(group);

                groups.add(group);

                group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        int groupId = group.getId();
                        HashMap map = new HashMap();

                        RadioButton checkedbutton = (RadioButton) group.findViewById(checkedId);
                        map.put(groupId+"", checkedbutton.getText().toString());
                        properties.add(map);

cartPropertiesList.add(checkedbutton.getText().toString());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值