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に他のビュー(などTextView)が含まれている場合、indexOfChild()メソッドは間違ったインデックスを返します。

選択RadioButtonしたテキストを取得するにはRadioGroup

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

これはうまくいくはずです、

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

そして、この空間的なラジオボタンが選択されるときはいつでも、あなたがそれを与えたIDによってその値を引き出すことができます

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

Kotlin:

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

パーティーに遅れましたが、これは@ Tarek360のKotlinの回答を簡略化したもので、RadioGroupRadioButton以外が含まれている可能性があります。

    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

これを使用するだけです:

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

グループにTextViewまたはRadioButton以外が含まれている場合でも、正しい位置を取得するためのKotlin拡張機能を次に示します。

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 に答える
1

できるよ

findViewById

ラジオグループから。

ここにサンプルがあります:

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);
于 2015-09-09T04:08:05.510 に答える
1

あなたは簡単にできます

-onCreateメソッドからラジオグループとラジオボタンを宣言します

private RadioGroup GrupName;
private RadioButton NameButton;

-onCreateメソッドでビューIDを設定します

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

-を使用して選択したラジオボタンIDを取得します

int id = GroupName.getCheckedRadioButtonId();

-idを使用して、ボタンで選択されたビューと一致させます

NameButton = (RadioButton) findViewById(id);

-最後にラジオボタンの値を取得します

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

--- PS:INT値が必要な場合は、それをキャストできます

int valueExpectedIn = Integer.parseInt(valueExpected);
于 2019-08-13T21:11:09.313 に答える
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が割り当てられています。ケースブロックを使用すると、選択したラジオボタンを手動で検出できます。また、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>

java:

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 に答える