
单选按钮要在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup。
RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下。
除了RadioButton,也允许放置其他控件。
单选组与线性布局相比,它们主要有以下两个区别:
(1)单选组多了管理单选按钮的功能,而线性布局不具备该功能;
(2)如果不指定orientation属性,那么单选组默认垂直排列,而线性布局默认水平排列;

判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。
下面是RadioGroup常用的3个方法:
(1)、check:选中指定资源编号的单选按钮。(2)、getCheckedRadioButtonId:获取选中状态单选按钮的资源编号。(3)、setOnCheckedChangeListener:设置单选按钮勾选变化的监听器。

==================================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择您的性别"
android:textColor="@color/black"
android:textSize="17sp" />
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizo

本文介绍了Android中RadioGroup组件的用途,它作为一个布局管理器用于实现单选按钮的选择。RadioGroup与线性布局的区别在于其管理单选按钮的功能和默认的垂直布局。文中展示了如何在XML中配置RadioGroup和RadioButton,并通过代码实现监听单选按钮的选中事件,动态更新TextView的内容。此外,还提供了设置单选监听器的方法。
最低0.47元/天 解锁文章
1403

被折叠的 条评论
为什么被折叠?



