[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()
        }
相关推荐
恋猫de小郭3 小时前
Flutter 3.35 发布,快来看看有什么更新吧
android·前端·flutter
雨白6 小时前
加密、签名与编码
android
李新_7 小时前
【Android Bug Fix】UI不响应、异位异常排查
android·程序员
帅得不敢出门8 小时前
Android Framework定制长按电源键关机的窗口
android·java·framework
爬山算法9 小时前
MySQL(191) 如何优化MySQL的磁盘I/O?
android·数据库·mysql
深盾安全11 小时前
Kotlin Data Classes 快速上手
android
一枚小小程序员哈12 小时前
安卓\android程序开发之基于 Android 的校园报修系统的设计与实现
android
用户20187928316713 小时前
🌟 一场失败的加密舞会:SSL握手失败的奇幻冒险
android
tangweiguo0305198714 小时前
面向对象编程三剑客:Dart、Java 和 Kotlin 的核心区别
android·flutter·kotlin
幼稚园的山代王15 小时前
Kotlin数据类型
android·开发语言·kotlin