229

有没有一种简单的方法可以在 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>

如果用户选择option 3我想获取索引,2。

4

19 回答 19

505

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

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

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

要在 上获取选定的RadioButton文本RadioGroup

 RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
 String selectedtext = r.getText().toString();
于 2011-06-22T13:58:12.687 回答
116

This should work,

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
于 2011-06-22T14:06:25.883 回答
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
    }
}
于 2011-06-22T13:11:49.383 回答
28

尝试这个

        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);
            }
        });
于 2015-04-20T06:49:13.863 回答
10

您可以使用 OnCheckedChangeListener 也可以使用 getCheckedRadioButtonId ()

于 2011-06-22T13:10:05.063 回答
9

您可以使用:

RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
于 2012-10-06T10:23:25.850 回答
6

//用于获取选中项的id

int selectedID = myRadioGroup.getCheckedRadioButtonId();

//获取选中项的视图

View selectedView = (View)findViewById( selectedID);
于 2013-05-23T07:52:33.863 回答
6

它以这种方式完美地为我工作:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    int radioButtonID = radioGroup.getCheckedRadioButtonId();
    RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
    String selectedtext = (String) radioButton.getText();
于 2016-10-28T09:16:09.600 回答
5

您只需要先为 RadioButton 设置值,例如:

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

and then whenever this spacific radioButton will be chosen you can pull its value by the Id you gave it with

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

干杯!

于 2017-03-14T17:38:30.660 回答
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();
     }
  });
于 2017-07-23T13:37:28.433 回答
4

科特林:

val selectedIndex = radioButtonGroup?.indexOfChild(
  radioButtonGroup?.findViewById(
    radioButtonGroup.getCheckedRadioButtonId())
)
于 2019-09-22T03:54:24.360 回答
3

派对迟到了,但这里是@Tarek360 的 Kotlin 答案的简化,它适合RadioGroup可能包含非 RadioButtons 的 s:

    val RadioGroup.checkedIndex: Int
        get() = children
            .filter { it is RadioButton }
            .indexOfFirst { it.id == checkedRadioButtonId }

如果你RadioFroup肯定只有RadioButtons 那么这可以很简单:

    private val RadioGroup.checkedIndex = 
        children.indexOfFirst { it.id == checkedRadioButtonId }

那么你就没有findViewById.

于 2020-08-23T10:50:45.903 回答
2
 int index_selected = radio_grp.indexOfChild(radio_grp
                .findViewById(radio_grp.getCheckedRadioButtonId()));
于 2014-12-22T14:10:13.703 回答
2

这是一个Kotlin扩展,即使您的组包含 TextView 或任何非 RadioButton ,也能获得正确的位置。

fun RadioGroup.getCheckedRadioButtonPosition(): Int {
    val radioButtonId = checkedRadioButtonId
    return children.filter { it is RadioButton }
        .mapIndexed { index: Int, view: View ->
            index to view
        }.firstOrNull {
            it.second.id == radioButtonId
        }?.first ?: -1
}
于 2020-03-07T08:42:16.077 回答
2

只需使用这个:

    int index = 2;
    boolean option3Checked = radioGroup.getCheckedRadioButtonId() == radioGroup.getChildAt(2).getId();
于 2017-04-27T14:07:29.113 回答
1

你可以简单地

- 在 onCreate 方法中声明单选组和单选按钮

private RadioGroup GrupName;
private RadioButton NameButton;

- 在 onCreate 方法中设置视图 id

GrupName = (RadioGroup) findViewById(R.id.radioGroupid);

- 使用选择的单选按钮 id

int id = GroupName.getCheckedRadioButtonId();

- 使用 id 来匹配 buttonselected 视图

NameButton = (RadioButton) findViewById(id);

-finally 得到单选按钮的值

String valueExpected = NameButton.getText().toString();

--- PS:如果你想要一个 INT 值,那么你可以铸造它

int valueExpectedIn = Integer.parseInt(valueExpected);
于 2019-08-13T21:11:09.313 回答
1

你可以做

findViewById

来自广播组。

这是示例:

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);
于 2015-09-09T04:08:05.510 回答
0
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();
            // find the radiobutton by returned id
            radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
        }
    });
于 2020-04-29T00:38:39.600 回答
0

您已经为每个单选按钮分配了一个 ID。使用 case 块,您可以手动检测选定的单选按钮,或者使用 OnCheckedChangeListener 您也可以得到它。

xml:

    <RadioGroup
        android:id="@+id/rd_group_pick_reason"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ti_titel"
        android:orientation="horizontal"
        android:paddingHorizontal="16dp"
        >
        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/rd_not_deliverable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/txt_not_deliverable"
            android:checked="true"
            />

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/rd_after_pack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/txt_after_pack"
            />
    </RadioGroup>

爪哇:

final RadioGroup radioGroup = dialogView.findViewById(R.id.rd_group_pick_reason);    
switch (radioGroup.getCheckedRadioButtonId()) {
    case R.id.rd_not_deliverable:
        // TODO
    break;
    case R.id.rd_after_pack:
        // TODO
    break;
}

或者

radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
    switch (group.getCheckedRadioButtonId()) {
        case R.id.rd_not_deliverable:
            System.out.println("not deliverable");
            break;
        case R.id.rd_after_pack:
            System.out.println("after pack");
            break;
    }
于 2021-10-07T07:59:46.797 回答