单选按钮

  • 更新时间:2026-03-14 22:04:43

单选按钮-radio

原生类型:{com.google.android.material.radiobutton.MaterialRadioButton}

单选按钮:一般需要配合单选按钮组(radio-group)的配合才能实现互相排斥的效果,这个控件常用在只需要在多个选择中选择一个的情况。

一、常用属性

color - 主题颜色

设置主题颜色

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio color="#3574F0" text="吃饭" />
        <radio color="#518855" text="睡觉" />
        <radio color="#B44A4A" text="游戏" />
    </radio-group>
</ui>

check - 选中状态

设置选中状态

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio color="#3574F0" text="吃饭" />
        <radio check="true" color="#518855" text="睡觉" />
        <radio color="#B44A4A" text="游戏" />
    </radio-group>
</ui>

text - 文本

设置文本

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio color="#3574F0" text="我是文本1" />
        <radio color="#518855" text="我是文本2" />
    </radio-group>
</ui>

textColor - 文本颜色

设置文本颜色

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio color="#3574F0" text="我是文本1" textColor="#57965C" />
        <radio color="#518855" text="我是文本2" textColor="#C94F4F" />
    </radio-group>
</ui>

minW - 最小宽度

设置最小宽度

<ui>
    <statusbar />
    <radio-group dir="v" w="max">
        <radio bg="#57965C" minW="100" text="最小宽度:100" />
        <radio bg="#3574F0" minW="150" text="最小宽度:150" />
        <radio bg="#C94F4F" minW="200" text="最小宽度:200" />
    </radio-group>
</ui>

minH - 最小高度

设置最小高度

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio bg="#57965C" minH="50" text="最小高度:50" />
        <radio bg="#3574F0" minH="60" text="最小高度:60" />
        <radio bg="#C94F4F" minH="70" text="最小高度:70" />
    </radio-group>
</ui>

padding - 内边距

设置内边距

<ui>
    <statusbar />
    <radio-group dir="v" w="max">
        <radio bg="#57965C" padding="10" text="内边距:10" />
        <radio bg="#3574F0" padding="20" text="内边距:20" />
        <radio bg="#C94F4F" padding="30" text="内边距:30" />
    </radio-group>
</ui>

gravity - 重力

设置重力

<ui>
    <statusbar />
    <!--将宽度设置到最大才能看到效果-->
    <radio-group dir="v" w="max">
        <radio gravity="start" text="开始" w="max" />
        <radio gravity="center" text="中间" w="max" />
        <radio gravity="end" text="结束" w="max" />
    </radio-group>
</ui>

bg - 背景颜色

设置背景颜色

<ui>
    <statusbar />
    <radio-group dir="v" w="max">
        <radio bg="#57965C" text="看我背景颜色" />
        <radio bg="#3574F0" text="看我背景颜色" />
    </radio-group>
</ui>

bgImg - 背景图片

设置背景图片

<ui>
    <statusbar />
    <radio-group dir="h" w="max">
        <radio bgImg="example/$ui - 交互界面/17.radio-单选按钮/img/img.png" color="bg"
            gravity="center" text="睡觉" textColor="bg" weight="0.2" />
        <radio bgImg="example/$ui - 交互界面/17.radio-单选按钮/img/img_1.png" color="bg"
            gravity="center" text="游戏" textColor="bg" weight="0.2" />
        <radio bgImg="example/$ui - 交互界面/17.radio-单选按钮/img/img_2.png" color="bg"
            gravity="center" text="吃饭" textColor="bg" weight="0.2" />
    </radio-group>
</ui>

二、常用函数

isChecked()

判断选中状态

  • 返回 : {boolean} 是否选中了
  • 版本 : 1.8.2
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//判断选中状态
if(radio.isChecked()){
    log("选中了");
}

check(checked)

设置选中状态

  • 参数 : checked {boolean} 是否选中
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置选中状态
radio.check(true);

onCheck(callback)

设置选中状态回调事件

  • 参数 : callback {(isChecked)=>{}} 回调事件
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置选中状态回调事件
radio.onCheck((isChecked)=>{
    //..
});

setGravity(gravity)

设置控件的对齐方式

  • 参数 : gravity {String} 例如:"center|bottom"
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置控件的对齐方式
radio.setGravity("center|bottom");

setText(text)

设置文字

  • 参数 : text {String} 文字
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置文字
radio.setText("吃饭");

getText()

获得文字

  • 返回 : {String} 文字
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//获得文字
let text = radio.getText();

setColor(color)

设置主题颜色

  • 参数 : color {String} 颜色
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置主题颜色
radio.setColor("#26282E");

setTextColor(color)

设置文字颜色

  • 参数 : color {String} 颜色
//解析布局,获得ui对象
let ui = $ui.layout("./mUi.xml");
//获取控件
let radio = ui.id("mRadio");
//设置文字颜色
radio.setTextColor("#26282E");