[Android]RadioButton控件

RadioButton控件


RadioButton控件是单选按钮控件,它继承自Button控件,可以直接使用Button控件支持的各种属性和方法。

与普通按钮不同的是,RadioButton控件多了一个可以选中的功能,能额外指定一个android:checked属性,该属性可以指定初始状态是否被选中。其实也可以不用指定,默认初始状态都不被选中。

RadioButton控件必须和单选框RedioGroup控件一起使用。

RadioButton控件的使用


复制代码
<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一年级"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="二年级"
            />
        <RadioButton
            android:id="@+id/three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="三年级"
            />
    </RadioGroup>

RadioButton控件的选中监听器


复制代码
//为RadioGroup设置一个事件监听器
        val radioGroup:RadioGroup=findViewById(R.id.radiogroup)
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            val check:RadioButton=findViewById(checkedId)
            Toast.makeText(this,"${check.text}",Toast.LENGTH_SHORT).show()
        }
相关推荐
e***5823几秒前
Spring Cloud GateWay搭建
android·前端·后端
x***13393 小时前
【MyBatisPlus】MyBatisPlus介绍与使用
android·前端·后端
n***54384 小时前
【MySQL】MySQL内置函数--日期函数字符串函数数学函数其他相关函数
android·mysql·adb
z***75155 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
android·前端·后端
程序员陆业聪5 小时前
Android模拟器检测全面指南:从基础到高级策略
android
2501_916008897 小时前
iOS 性能测试的深度实战方法 构建从底层指标到真实场景回放的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
w***95497 小时前
SQL美化器:sql-beautify安装与配置完全指南
android·前端·后端
r***12388 小时前
若依微服务中配置 MySQL + DM 多数据源
android·mysql·微服务
ALex_zry8 小时前
MySQL连接数管理与优化实操经验分享
android·mysql·adb
apigfly10 小时前
深入Android系统(十三)Android的窗口系统
android·设计模式·源码