如何获得RadioGroup中的Android中选定的索引 [英] How to get the selected index of a RadioGroup in Android
本文介绍了如何获得RadioGroup中的Android中选定的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个简单的方法来得到一个RadioGroup中Android中的选择的索引,我必须用OnCheckedChangeListener侦听变化,有一些保存选择的最后一个索引?
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?
例如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。
if a user selects option 3
I want to get the index, 2.
推荐答案
您应该能够做这样的事情:
You should be able to do something like this:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
如果在 RadioGroup中
包含其他视图(如的TextView
),那么 indexOfChild( )
方法将错误返回的索引。
If the RadioGroup
contains other Views (like a TextView
) then the indexOfChild()
method will be returned wrong the index.
这篇关于如何获得RadioGroup中的Android中选定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文