如何获取动态添加的单选按钮的选定索引

问题描述:

我已经像下面那样动态地添加了单选按钮。如何获取动态添加的单选按钮的选定索引

for (int i = 0; i< typeArrayList.size(); i++) { 
        radioButtons[i] = new RadioButton(MainActivity.this); 
        radioButtons[i].setId(i); 
        radioButtons[i].setText(typeArrayList.get(i).toString()); 
        if(i==0) { 
         radioButtons[i].setChecked(true); 
        } 
        typeLayout.addView(radioButtons[i]); 
       } 

点击时有一个按钮调用一个方法,动态添加的单选按钮的选定项目(文本)应该通过该方法。如何获取动态添加的单选按钮的选定单选按钮文本?

+0

您是否检查我们的解决方案 –

您已将所有您创建的RadioButtons动态存储在radioButtons array中。
所以,如果你想知道哪个单选按钮被选中,你可以循环所有这些阵列,并检查各RadioButton

for (int i = 0; i< radioButtons.size(); i++) { 
    if(radioButtons[i].isChecked()){ 
     // selected radio button is here 
     String text = radioButtons[i].getText(); 
    } 
} 

这可以通过使用标签可以简单地实现。

虽然添加了RadioButton设置了标记该特定对象。在你的情况,设置标签为单选按钮的文字,

radioButtons[i].setTag(typeArrayList.get(i).toString()); 

这样,所有的单选按钮都会有一个标签与之关联文本。

现在无论何时选择一个特定的单选按钮,只需获取标签,该标签将为您提供与其相关的文本。

String text = (String) selectedRadioButton.getTag(); 

希望它有帮助。

首先,你必须得到你的看法的所有孩子。然后检查这个视图是否是RadioButton。最后检查哪个按钮被选中。

int childcount = typeLayout.getChildCount(); 
for (int i=0; i < childcount; i++){ 
     View view = typeLayout.getChildAt(i); 
     if (view instanceof RadioButton) { 
      if(((RadioButton)view).isChecked()) { 
       RadioButton yourCheckedRadioButton = (RadioButton) typeLayout.getChildAt(i); // this is your checked RadioButton 
      } 
     } 
} 

@Komali Shirumavilla

添加这些动态单选按钮单选组 所以添加以下代码中的行

RadioGroup rg = new RadioGroup(this); //create the RadioGroup 
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
for (int i = 0; i< typeArrayList.size(); i++) { 
radioButtons[i] = new RadioButton(MainActivity.this); 
radioButtons[i].setId(i); 
radioButtons[i].setText(typeArrayList.get(i).toString()); 
if(i==0) { 
    radioButtons[i].setChecked(true); 
} 
rg.addView(radioButtons[i]); // add dynamic radio buttons to radio group 
} 
typeLayout.addView(rg); // add radio group to view 

现在设置setOnCheckedChangeListenerRadioGroup

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch(checkedId) { 
        RadioButton btn = (RadioButton)findViewById(checkedId); 
        Log.d("Your selected radio button id",btn.getText()); 
       } 
      } 
     }); 

它对s很重要等你的单选按钮ID添加按钮时,收音机组。

RadioButton rb = new RadioButton(context); 
rb.setText(option); 
rb.setId(rb.hashCode()); 
radioGroup.addView(rb); 
radioGroup.setOnCheckedChangeListener(mCheckedListner); 

现在在您的点击监听器中检查唯一的ID。

private RadioGroup.OnCheckedChangeListener mCheckedListner = new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     JSONArray actionArray = new JSONArray(); 
     if(group.findViewById(checkedId)!=null) { 
      RadioButton rb = ((RadioButton) group.findViewById(checkedId)).getText(); 
//Your Code here 
       } 
      } 
     } 
    };