Android 学习之- 单选按钮、复选框、状态开关、时钟控件

本文介绍了如何在Android应用中使用单选按钮、复选框和状态开关,通过实例展示了它们的基本用法和配置。

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


//项目源码附上,可以下载共享!


<!--
单选按钮操作测试:
1、单选按钮:RadioButton 需要配合 RadioGroup 进行使用,RadioGroup 是 RadioGroup 的承载体
2、每一组RadioGroup 里面只能有一个RadioButton 被选中,不同的组之间互不影响;
3、一个RadioGroup 里面至少包含两个RadioButton , 能包含多个单选按钮;
4、每一组RadioGroup 中都有一个默认的被选中的单选按钮,大部分的情况下建议选择第一个为默认选择
案例测试:选择喜欢的颜色

-->
<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:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选按钮测试" />

<!-- 单选按钮的组 , 里面包含的是三个单选按钮 , 进行选择的测试 -->

<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/radioRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="红色" />

<RadioButton
android:id="@+id/radioGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色" />

<RadioButton
android:id="@+id/radioBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="黑色" />
</RadioGroup>

<!-- 复选框的测试 -->
<!--
一组: CheckBox 能同时的选择多个,每次点击的时候可以选择是否被选中,在UI 中默认的是以矩形的方式显示;
事件:CompoundButton.OnCheckedChangeListener 后部分与按钮 状态控件的事件名称一致,加以不同的前缀进行区别

-->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="请选择您的爱好"
android:textStyle="normal" />

<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳" />

<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="散步" />

<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书" />

<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />

<!-- 状态开关测试 -->
<!--
控件名称:ToggeButton
属性:设置开/关的时候对应的文本的值:textOn/textOff

-->
<!-- 开关转换器 默认是开 纵向 -->

<ToggleButton
android:id="@+id/toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列"
android:textOn="纵向排列" />

<!-- 使用线性布局结合状态栏的排列 -->

<LinearLayout
android:id="@+id/linears"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clock1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clock2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clock3" />
</LinearLayout>

</LinearLayout>


<!--
时钟测试
AnalogClock 模拟时钟
DigitalClock 数字时钟
-->
<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:background="@drawable/a"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时钟测试" />

<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

<!-- 数字时钟 -->
<!--
时钟(AnalogClock和DigitalClock)的功能和用法:
AnalogClock继承了View组件,重写了View的OnDraw方法,它会在View上显示模拟时钟。
DigitalClock本身就继承了TextView,也就是说它本身就是文本框,只是它里面显示的内容是当前时间。
AnalogClock和DigitalClock都会显示当前时间,不同的是;
DigitalClock显示数字时钟,可以显示当前的秒数;
AnalogClock显示模拟时钟,不会显示当前秒数;
-->

<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值