一步一步学android之基本控件——RadioGroup组件

单选按钮在开发中提供了一种多选一的操作模式,也是比较常见的一种组件,例如在选择性别的时候,在Android中可以使用<RadioGroup>标签来定义单选按钮组件,它的定义如下http://developer.android.com/reference/android/widget/RadioGroup.html):


我们可以看见他继承于LinearLayout(这个后面会说到),所以RadioGroup也可以使用LinearLayout的属性,如orientation属性(控制内部子组件的显示方向),这里要注意的是RadioGroup提供的只是一个容器,需要在里面加RadioButton才可以出现单选按钮,RadioButton的定义如下http://developer.android.com/reference/android/widget/RadioButton.html):


发现他继承于CompoundButton(继承于Button),所以RadioButton的使用可以说和Button是类似的,只是RadioButton在定义时候必须放进容器RadioGroup中(切记),同样下面写个例子来说明。

运行效果图:


main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/colortv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢什么颜色:" />

    <RadioGroup
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/red"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/red"
            android:text="红色"
            android:textColor="#ff0000" />

        <RadioButton
            android:id="@+id/black"
            android:text="黑色" />

        <RadioButton
            android:id="@+id/green"
            android:text="绿色"
            android:textColor="#00ff00" />
    </RadioGroup>

    <TextView
        android:id="@+id/sextv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你的性别是:" />

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@+
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值