如何在Android中获取RadioGroup的选中索引

236

在Android中,有没有一种简单的方法来获取RadioGroup的选定索引,或者我必须使用OnCheckedChangeListener来监听更改并拥有一个持有最后选择的索引的变量?

示例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>
如果用户选择了选项3,我想要获取它的索引,也就是2。
19个回答

517

你应该可以像这样做:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
如果一个 RadioGroup 包含其他的 View (例如 TextView),那么使用 indexOfChild() 方法会返回错误的索引。
要获取 RadioGroup 中选中的 RadioButton 文本,请执行以下操作:
 RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
 String selectedtext = r.getText().toString();

11
如果这些按钮没有设置它们的“android:id”属性,该怎么办? - neuront
@BP 当没有设置父元素或单选按钮的ID时,我在访问单选按钮方面有同样的疑问。 - Shubham AgaRwal
2
只要你使用radioGroup.findViewById(radioButtonID),它就会起作用。RadioGroup将1、2、3、4等设置为视图的ID,因此如果在其上下文中搜索它们,它们就会起作用。 - Reinherd
如果默认的(且未被用户更改)单选按钮被设置,这将无法正常工作。 - Ninja Coding
2
如果你和我犯了同样的错误,你需要通过调用radioGroup.check(selectedRadioButton.id)来设置默认(初始)单选按钮,而不是radioButton.setChecked(true)。 - Lensflare

117

这应该可以工作,

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

6
如果你在Fragment中使用,可以使用getActivity().findViewById()。 - Sanjeet A

56
你可以创建一个对单选按钮组的引用,并使用getCheckedRadioButtonId()来获取选中的单选按钮id。点击这里查看详细信息。
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
    }
}

1
是的,这是选中的单选按钮的ID,但单选按钮在单选组中的索引呢? - John Boker
如果我有一个包含5个单选按钮的单选组,并且用户选择了第3个单选按钮,我想要获取2,即单选组中所选单选按钮的索引。这不是一个列表视图。 - John Boker
只是为了澄清,如果用户选择第三个按钮,您想要获取第二个按钮吗?单选组和按钮没有索引。 - Stefan Bossbaly
不,第三个按钮的索引是2,基于0的索引。第一个单选按钮的索引为0,第二个为1,第三个为2,以此类推...你的意思是没有好的方法来获取索引,这也是一个有效的答案。 - John Boker
您可以将 RadioButton 放入一个数组列表中,然后运行一个搜索 id 并将其转换为索引的过程。 - Stefan Bossbaly
显示剩余3条评论

29

试试这个

        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);
            }
        });

这个可以工作!但我不明白为什么我们需要使用这个“radioButton”视图.. - AlexS

10

9

您可以使用:

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

6

在我的情况下,它完美地发挥了作用:

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

6

//用于获取所选项的ID

int selectedID = myRadioGroup.getCheckedRadioButtonId();

//获取所选项目的视图
View selectedView = (View)findViewById( selectedID);

5
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();
     }
  });

5
Kotlin:
val selectedIndex = radioButtonGroup?.indexOfChild(
  radioButtonGroup?.findViewById(
    radioButtonGroup.getCheckedRadioButtonId())
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接