[Android]RadioButton控件

简介: [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()
        }
目录
相关文章
|
24天前
|
XML 数据格式
Android-自定义三角形评分控件
Android-自定义三角形评分控件
15 0
|
1月前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
27 4
|
29天前
|
XML Android开发 数据格式
Android基础控件介绍
Android基础控件介绍
14 0
|
1月前
|
XML Java Android开发
16. 【Android教程】选择框 RadioButton/Check
16. 【Android教程】选择框 RadioButton/Check
17 3
|
1月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
39 2
|
1月前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
27 2
|
1月前
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
23 0
|
2月前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
20 1
|
2月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
181 1
|
2月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配