[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()
        }
相关推荐
AI玫瑰助手1 分钟前
Python函数:def定义函数与参数传递基础
android·开发语言·python
Maddie_Mo8 分钟前
Pi Agent Web 使用教程:把本地 Pi Coding Agent 搬进浏览器
android·java·前端·人工智能·ai
码不停蹄的玄黓27 分钟前
MySQL索引设计核心注意事项
android·数据库·mysql
ch_ziyuan35 分钟前
安卓APP报毒自动化解决方案处理系统:动态包名+证书随机+360加固集成(后台源码)
android·运维·自动化
一拳一个娘娘腔1 小时前
【SRC漏洞挖掘系列】第11期:移动端安全(Android/iOS)—— APP 里的“猫腻”大起底
android·安全·ios
Jomurphys1 小时前
Compose 调用 - 液态玻璃 Backdrop
android·compose
风别鹤2 小时前
Android Studion Gradle与AGP版本管理
android·ide·android studio
程序员陆业聪2 小时前
WebView白屏检测与解决方案:从原因分析到工程化监控
android
程序员陆业聪2 小时前
WebView内核原理:从Chromium到System WebView的架构全景
android
aykon2 小时前
Android app启动速度优化
android·性能优化