[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()
        }
相关推荐
观无30 分钟前
pinia-plugin-persist、vuex
android·flutter
dora2 小时前
逼格提起来,使用curl发送网络请求
android·c++
tangweiguo030519872 小时前
Android Compose 中获取和使用 Context 的完整指南
android·compose
wss2 小时前
Android 与 Unity 集成实现,思必驰 TTS 音频流处理及口型同步
android
IT技术图谱2 小时前
【绝非标题党】Android一行代码实现网络监听
android·程序员
louisgeek4 小时前
Android 性能优化之界面优化
android
末央&4 小时前
【C++】vector的底层封装和实现
android·c++
希侬4 小时前
android自定义Toast样式和显示方式
android
希侬4 小时前
Android国内大厂推送规范整理
android
Aric5 小时前
conan cross build for Android with NDK toolchain
android