动态添加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());