HarmonyOS —常用组件开发指导-RadioButton

HarmonyOS —常用组件开发指导-RadioButton

RadioButton用于多选一的操作,需要搭配RadioContainer使用,实现单选效果。

支持的XML属性

RadioButton的共有XML属性继承自:Text

RadioButton的自有XML属性见下表:

RadioButton用于多选一的操作,需要搭配RadioContainer使用,实现单选效果。

支持的XML属性

RadioButton的共有XML属性继承自:Text

RadioButton的自有XML属性见下表:

创建RadioButton

在layout目录下的xml文件中创建RadioButton。

<RadioButton
    ohos:id="$+id:rb_1"
    ohos:height="40vp"
    ohos:width="match_content"
    ohos:text="A.Learning"
    ohos:text_size="20fp"/>

图1 RadioButton效果

设置RadioButton

设置单选按钮的字体颜色

  • 在xml中设置:text_color_on为选中状态的字体颜色,text_color_off为未选中状态的字体颜色。
<RadioButton
    ...
    ohos:text_color_on="#00BFFF"
    ohos:text_color_off="#808080"/>

或者在Java代码中设置:

rBtn.setTextColorOn(new Color(Color.getIntColor("#0066FF")));
rBtn.setTextColorOff(new Color(Color.getIntColor("#505050")));

图2 设置按钮选中前后的字体颜色效果

设置状态标志样式

  • 在xml中设置:
<RadioButton
    ...
    ohos:check_element="$graphic:checkbox_check_element"
/>

graphic目录下checkbox_check_element.xml的示例如下:

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <item
        ohos:element="$graphic:background_checkbox_checked"
        ohos:state="component_state_checked"/>
    <item
        ohos:element="$graphic:background_checkbox_empty"
        ohos:state="component_state_empty"/>
</state-container>

graphic目录下background_checkbox_checked.xml的示例如下:

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <solid
        ohos:color="#007DFF"/>
    <corners
        ohos:radius="4"/>
</shape>

graphic目录下background_checkbox_empty.xml的示例如下:

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <stroke ohos:width="5"
            ohos:color="gray"/>
    <corners
        ohos:radius="4"/>
</shape>

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x