[英]Deselect dynamically generated radio buttons after selecting one radio button in android
我正在從服務器在android中返回的json數據動態生成單選按鈕。
我可以在單選組中顯示單選按鈕。 也可以獲取每個單選按鈕的ID。
但是,當我單擊下一個單選按鈕時,所有按鈕均保持選中狀態。 它們都不是未選中的。
由於無線電號碼是動態生成的,因此其大小會有所不同。
我的代碼段:
if(type.equals("radio_buttons")){
String optionName = regData.getOption();
optionName = Character.toString(optionName.charAt(0)).toUpperCase()+optionName.substring(1);
List listItem = new ArrayList();
listItem.add(optionName);
final RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);
final int radioSize = listItem.size();
final RadioButton[] rb = new RadioButton[radioSize];
for(int i=0; i<radioSize; i++){
rb[i] = new RadioButton(getActivity());
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setId(i);
rb[i].setText(optionName);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int j = 0; j<radioSize; j++){
rg.removeViewAt(checkedId);
}
/*
switch (checkedId)
{
case 1:
Toast.makeText(OnsiteRegistrationFragment.this.getActivity(), "VIEW ID " + checkedId, Toast.LENGTH_LONG).show();
case 2:
Toast.makeText(OnsiteRegistrationFragment.this.getActivity(), "VIEW ID " + checkedId, Toast.LENGTH_LONG).show();
}*/
}
});
this.linearLayout.addView(rg);
}
在Android中選擇一個單選按鈕時,如何清除/取消選中其他單選按鈕?
LinkedList<RadioButton> radiobuttons
在創建按鈕時,在for循環的單選按鈕中添加單選按鈕。
for (int i = 0; i < radioSize.size(); i++) {
RadioButton rb;
rb = radiobuttons.get(i);
rg.removeView(rb);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.