是否有一种简单的方法可以在Android中获取RadioGroup的选定索引,或者是否必须使用OnCheckedChangeListener来监听更改并确保选定最后一个索引?如何在Android中获取RadioGroup的选定索引
示例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
但是如果这些按钮没有设置“android:id”属性呢? – neuront
@BP我没有设置任何父级或单选按钮ID,所以我在访问单选按钮时有同样的疑问。 – Killer
@neuront只要你做radioGroup.findViewById(radioButtonID)它会工作。 RadioGroup确实将1,2,3,4等设置为视图的ID,所以如果你在它的上下文中搜索它们,它将工作 – Reinherd