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

Android 如何获取动态添加单选按钮的选定索引,android,Android,我动态添加了单选按钮,如下所示 for (int i = 0; i< typeArrayList.size(); i++) { radioButtons[i] = new RadioButton(MainActivity.this); radioButtons[i].setId(i); radioButtons[i].setText(typeArrayList.get(i).t

我动态添加了单选按钮,如下所示

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]);
                }
for(int i=0;i

单击时,我有一个按钮调用一个方法,动态添加的单选按钮的选定项(文本)应传递给该方法。如何为动态添加的单选按钮获取所选单选按钮文本?

您已将动态创建的所有
单选按钮存储在
单选按钮数组中

因此,如果您想知道选择了哪个radiobutton,可以循环所有该数组并检查每个
radiobutton

for (int i = 0; i< radioButtons.size(); i++) {
    if(radioButtons[i].isChecked()){
         // selected radio button is here
         String text = radioButtons[i].getText();
    }
}
for(int i=0;i
这可以通过使用标记来实现

添加
单选按钮时
设置该特定对象的标记。在您的情况下,将标记设置为单选按钮的文本

radioButtons[i].setTag(typeArrayList.get(i).toString());
这样,所有的单选按钮都将与之关联的文本作为标记

现在,每当选择一个特定的单选按钮时,只需获取标签,它将为您提供与其相关的文本

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

希望有帮助。

首先,你必须让所有孩子都了解你的观点。然后检查此视图是否为单选按钮。最后检查哪个按钮被选中

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
           }
      }
}
int childcount=typeLayout.getChildCount();
for(int i=0;i
@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

在向单选组添加按钮时,设置单选按钮id非常重要

RadioButton rb = new RadioButton(context);
rb.setText(option);
rb.setId(rb.hashCode());
radioGroup.addView(rb);
radioGroup.setOnCheckedChangeListener(mCheckedListner);
现在在您的click listener中检查唯一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
                }
            }
        }
    };

你检查过我们的解决方案了吗
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
                }
            }
        }
    };