前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学鸿蒙应用开发(38)- 修改RadioButton默认动作

自学鸿蒙应用开发(38)- 修改RadioButton默认动作

作者头像
面向对象思考
发布2021-05-11 14:33:47
3300
发布2021-05-11 14:33:47
举报

鸿蒙系统中RadionButton的默认动作是点击某个选项时出现选中标记,再次点击时取消选中状态,表现如下面视频中的第一排RadioButton。有时可能希望再次点击时可以维持选中状态,就像下面视频中第二排RadioButton那样。

布局文件

文件中简单地放置了两个包含3个RadioButton的RadioContainer。

代码语言:javascript
复制
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">
    <RadioContainer
        ohos:id="$+id:radio_container1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:top_margin="32vp"
        ohos:orientation="horizontal"
        ohos:layout_alignment="horizontal_center">
        <RadioButton
            ohos:id="$+id:radio_button_1"
            ohos:height="40vp"
            ohos:width="match_content"
            ohos:text="A.Learning"
            ohos:text_size="14fp"/>
        <RadioButton
            ohos:id="$+id:radio_button_2"
            ohos:height="40vp"
            ohos:width="match_content"
            ohos:text="B.Innovation"
            ohos:text_size="14fp"/>
        <RadioButton
            ohos:id="$+id:radio_button_3"
            ohos:height="40vp"
            ohos:width="match_content"
            ohos:text="C.Benefit"
            ohos:text_size="14fp"/>
    </RadioContainer>
    <RadioContainer
        ohos:id="$+id:radio_container2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:top_margin="32vp"
        ohos:orientation="horizontal"
        ohos:layout_alignment="horizontal_center">
        <RadioButton
            ohos:id="$+id:radio_huawei"
            ohos:height="60vp"
            ohos:width="match_content"
            ohos:text="H.Huawei"
            ohos:text_size="14fp"/>
        <RadioButton
            ohos:id="$+id:radio_xiaomi"
            ohos:height="60vp"
            ohos:width="match_content"
            ohos:text="X.Xiaomi"
            ohos:text_size="14fp"/>
        <RadioButton
            ohos:id="$+id:radio_oppo"
            ohos:height="60vp"
            ohos:width="match_content"
            ohos:text="O.Oppo"
            ohos:text_size="14fp"/>
    </RadioContainer>
</DirectionalLayout>

修改RadionButton的行为

下面的代码在RadioButton的选中状态发生变化时修改按钮是否接受单击的属性。

代码语言:javascript
复制
代码语言:javascript
复制
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        AbsButton.CheckedStateChangedListener listener = new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                absButton.setClickable(!b);
            }
        };
        RadioButton rb1 = (RadioButton)findComponentById(ResourceTable.Id_radio_huawei);
        rb1.setCheckedStateChangedListener(listener);
        RadioButton rb2 = (RadioButton)findComponentById(ResourceTable.Id_radio_xiaomi);
        rb2.setCheckedStateChangedListener(listener);
        RadioButton rb3 = (RadioButton)findComponentById(ResourceTable.Id_radio_oppo);
        rb3.setCheckedStateChangedListener(listener);
    }
}

参考代码

完整代码可以从以下链接下载:

https://github.com/xueweiguo/Harmony/tree/master/RadioButtonTest

作者著作介绍

《实战Python设计模式》是作者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档