Java 如何在 Android 中获取 RadioGroup 的选定索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6440259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the selected index of a RadioGroup in Android
提问by John Boker
Is there an easy way to get the selected index of a RadioGroup in Android or do I have to use OnCheckedChangeListener to listen for changes and have something that holds the last index selected?
有没有一种简单的方法可以在 Android 中获取 RadioGroup 的选定索引,或者我是否必须使用 OnCheckedChangeListener 来侦听更改并选择保存最后一个索引的内容?
example xml:
示例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>
if a user selects option 3
I want to get the index, 2.
如果用户选择option 3
我想获取索引,2。
采纳答案by BP.
You should be able to do something like this:
你应该能够做这样的事情:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
If the RadioGroup
contains other Views (like a TextView
) then the indexOfChild()
method will return wrong index.
如果RadioGroup
包含其他视图(如 a TextView
),则该indexOfChild()
方法将返回错误的索引。
To get the selected RadioButton
text on the RadioGroup
:
要在 上获取所选RadioButton
文本RadioGroup
:
RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();
回答by ingsaurabh
You can either use OnCheckedChangeListener or can use getCheckedRadioButtonId()
您可以使用 OnCheckedChangeListener 或使用 getCheckedRadioButtonId ()
回答by Stefan Bossbaly
You could have a reference to the radio group and use getCheckedRadioButtonId ()
to get the checked radio button id. Take a look here
您可以引用无线电组并用于getCheckedRadioButtonId ()
获取选中的单选按钮 ID。看看这里
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);
Then when you need to get the selected radio option.
然后当您需要获取选定的无线电选项时。
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == -1) {
// No item selected
}
else{
if (checkedRadioButtonId == R.id.radio_button1) {
// Do something with the button
}
}
回答by source.rar
This should work,
这应该有效,
int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
回答by Marco Fantasia
You can use:
您可以使用:
RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
回答by Jhoe-mar Pagao
//use to get the id of selected item
//用于获取选中项的id
int selectedID = myRadioGroup.getCheckedRadioButtonId();
//get the view of the selected item
//获取选中项的视图
View selectedView = (View)findViewById( selectedID);
回答by Prashant Maheshwari Andro
int index_selected = radio_grp.indexOfChild(radio_grp
.findViewById(radio_grp.getCheckedRadioButtonId()));
回答by Nooh
try this
尝试这个
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);
}
});
回答by Faheem
you can do
你可以做
findViewById
from the radio group .
来自广播组。
Here it is sample :
这是示例:
((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);
回答by Shinz6
It worked perfectly for me in this way:
它以这种方式对我来说非常有效:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
String selectedtext = (String) radioButton.getText();