2011-06-22 152 views
166

是否有一种简单的方法可以在Android中获取RadioGroup的选定索引,或者是否必须使用OnCheckedChangeListener来监听更改并确保选定最后一个索引?如何在Android中获取RadioGroup的选定索引

示例XML:

<RadioGroup android:id="@+id/group1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
    <RadioButton android:id="@+id/radio1" android:text="option 1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio2" android:text="option 2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio3" android:text="option 3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio4" android:text="option 4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio5" android:text="option 5" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
</RadioGroup> 

如果用户选择option 3我想要得到的指数,2

回答

395

你应该能够做这样的事情:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId(); 
View radioButton = radioButtonGroup.findViewById(radioButtonID); 
int idx = radioButtonGroup.indexOfChild(radioButton); 

如果RadioGroup包含其他视图(如TextView),则indexOfChild()方法将返回错误的索引。

获取所选的单选按钮文本上RadioGroup中

RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx); 
String selectedtext = r.getText().toString(); 
+7

但是如果这些按钮没有设置“android:id”属性呢? – neuront

+0

@BP我没有设置任何父级或单选按钮ID,所以我在访问单选按钮时有同样的疑问。 – Killer

+0

@neuront只要你做radioGroup.findViewById(radioButtonID)它会工作。 RadioGroup确实将1,2,3,4等设置为视图的ID,所以如果你在它的上下文中搜索它们,它将工作 – Reinherd

49

你可以有一个参考到收音机组并使用getCheckedRadioButtonId()获取选中的单选按钮ID。看看here

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group); 

然后,当你需要获得所选择的无线选项。

int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId(); 
if (checkedRadioButtonId == -1) { 
    // No item selected 
} 
else{ 
    if (checkedRadioButtonId == R.id.radio_button1) { 
     // Do something with the button 
    } 
} 
+0

是的,这是选中的单选按钮的ID,但是收音机组中的单选按钮的索引呢? –

+0

我可以得到id,我想索引,他们是不同的。 –

+0

你指的是什么意思?它是在列表视图中吗? –

97

这应该工作,

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId())); 
+2

如果您在片段中使用getActivity()。findViewById()。 –

9

您可以使用:

RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
5

//用它来获得所选项目的ID

int selectedID = myRadioGroup.getCheckedRadioButtonId(); 

//获取所选项目的视图

View selectedView = (View)findViewById(selectedID); 
2
int index_selected = radio_grp.indexOfChild(radio_grp 
       .findViewById(radio_grp.getCheckedRadioButtonId())); 
18

试试这个

 RadioGroup group= (RadioGroup) getView().findViewById(R.id.radioGroup); 
     group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int i) { 
       View radioButton = radioGroup.findViewById(i); 
       int index = radioGroup.indexOfChild(radioButton); 
      } 
     }); 
1

你可以从无线电集团做

findViewById 

这是样品:

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true); 
3

它的工作非常适合我这样:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group); 
    int radioButtonID = radioGroup.getCheckedRadioButtonId(); 
    RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID); 
    String selectedtext = (String) radioButton.getText(); 
3

所有你需要的是先设定值的单选按钮,例如:

RadioButton radioButton = (RadioButton)findViewById(R.id.radioButton);  
radioButton.setId(1);  //some int value 

然后当这个间隔的radioButton被选中时,你可以通过你给它的Id来拉取它的值与

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);      
int whichIndex = radioGroup.getCheckedRadioButtonId(); //of course the radioButton 
                 //should be inside the "radioGroup" 
                 //in the XML 

干杯!

1

只是使用:

int index = 2; 
    boolean option3Checked = radioGroup.getCheckedRadioButtonId() == radioGroup.getChildAt(2).getId(); 
3

radioSexGroup =(RadioGroup中)findViewById(R.id.radioGroup);

btnDisplay=(Button)findViewById(R.id.button); 

    btnDisplay.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     int selectedId=radioSexGroup.getCheckedRadioButtonId(); 
     radioSexButton=(RadioButton)findViewById(selectedId); 
     Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show(); 
    } 
    }); 
相关问题