[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()
        }
相关推荐
三8442 小时前
PHP Session 反序列化(2):漏洞根因 —— 序列化处理器错位与对象注入
android·web安全·php·反序列化·session
天空之城--2 小时前
Android行业一周动态:编码趋势与行业资讯汇总
android·性能优化·架构·kotlin·android jetpack
Co_Hui4 小时前
Android Handler 内存泄漏分析
android
张小姐的猫5 小时前
【AI大模型接入SDK】 —— Gemini接入封装
android·数据结构·数据库·c++·人工智能·python
Anhty8 小时前
叮咚变声器上手实测,iOS短板、安卓悬浮窗使用感受分享
android·人工智能·功能测试·ios·智能手机
方白羽9 小时前
OkHttp 5.3 隐形变更引发的线上偶发崩溃复盘
android·app·客户端
EatFan10 小时前
【实战经验】uni-app使用 SSE 踩坑,EventSource不支持怎么办?
android·后端·ios·uni-app
JMchen10 小时前
第 2 篇|Kotlin 进阶 —— 集合、循环与条件表达式
android·kotlin
律宏阔11 小时前
Android 自定义 Launcher 加载第三方 App Widget
android
律宏阔11 小时前
Android 9 开发板实现系统侧滑返回
android