Day4-Android单选、多选

本文详细介绍了Android开发中单选按钮(RadioButton)与多选框(CheckBox)的使用方法及XML布局示例,包括如何创建单选按钮组实现互斥选择功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单选:

RadioButton:

 单选按钮: 如果想要有互斥效果必须配合RadioGroup使用

   是TextView的子类

属性:android:checked="" 设置默认选择 true 表示选中 false 表示 不选中

在代码中可通过实例化对象radioButton调用 setChecked方法达到同样的效果

RadioGroup:

 单选按钮组:如果RadioButton 想要有互斥效果则必须配合RadioGroup使用

  是LinearLayout的子类 横向或者纵向排列

代码:

<TextView
    android:layout_width="wrap_content"
    android:text="性别"
    android:layout_marginTop="5dp"
    android:textSize="20sp"
    android:layout_height="wrap_content" />
<RadioGroup android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:text="男" android:layout_height="wrap_content" /> <RadioButton android:layout_width="wrap_content" android:text="女" android:layout_height="wrap_content" /></RadioGroup>

效果图:



多选:

CheckBox:选择框

 可以选中,再次点击则取消选中状态。

代码:

<TextView
    android:layout_width="wrap_content"
    android:text="爱好"
    android:layout_marginTop="5dp"
    android:textSize="20sp"
    android:layout_height="wrap_content" />

<CheckBox
    android:layout_width="wrap_content"
    android:text="篮球"
    android:layout_height="wrap_content" />
<CheckBox
    android:layout_width="wrap_content"
    android:text="乒乓球"
    android:layout_height="wrap_content" />
     <CheckBox
    android:layout_width="wrap_content"
    android:text="羽毛球"
    android:layout_height="wrap_content" />

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值