中级控件——单选按钮RadioButton——horizontal方向

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

单选按钮要在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值