[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()
        }
相关推荐
mygljx6 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
xinhuanjieyi7 小时前
ruoyimate导入sql\antflow\bpm_init_db.sql报错
android·数据库·sql
闲猫9 小时前
基于RABC的权限控制设计
android
星霜笔记12 小时前
GitMob — 手机端 GitHub 管理工具
android·kotlin·github·android jetpack
LiuYaoheng12 小时前
问题记录:Android Studio Low memory
android·ide·android studio
独隅13 小时前
Python 标准库 (Standard Library) 全面使用指南
android·开发语言·python
always_TT13 小时前
strlen、strcpy、strcat等常用字符串函数
android
qqty121713 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
2401_8955213413 小时前
MySQL中between and的基本用法
android·数据库·mysql
云云鬼才14 小时前
CoCo编辑器、图形化编程怎么调用Scheme(跳转应用)
android