自学鸿蒙应用开发(38)- 修改RadioButton默认动作
鸿蒙系统中RadionButton的默认动作是点击某个选项时出现选中标记,再次点击时取消选中状态,表现如下面视频中的第一排RadioButton。有时可能希望再次点击时可以维持选中状态,就像下面视频中第二排RadioButton那样。
布局文件
文件中简单地放置了两个包含3个RadioButton的RadioContainer。
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><RadioContainerohos:id="$+id:radio_container1"ohos:height="match_content"ohos:width="match_content"ohos:top_margin="32vp"ohos:orientation="horizontal"ohos:layout_alignment="horizontal_center"><RadioButtonohos:id="$+id:radio_button_1"ohos:height="40vp"ohos:width="match_content"ohos:text="A.Learning"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_button_2"ohos:height="40vp"ohos:width="match_content"ohos:text="B.Innovation"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_button_3"ohos:height="40vp"ohos:width="match_content"ohos:text="C.Benefit"ohos:text_size="14fp"/></RadioContainer><RadioContainerohos:id="$+id:radio_container2"ohos:height="match_content"ohos:width="match_content"ohos:top_margin="32vp"ohos:orientation="horizontal"ohos:layout_alignment="horizontal_center"><RadioButtonohos:id="$+id:radio_huawei"ohos:height="60vp"ohos:width="match_content"ohos:text="H.Huawei"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_xiaomi"ohos:height="60vp"ohos:width="match_content"ohos:text="X.Xiaomi"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_oppo"ohos:height="60vp"ohos:width="match_content"ohos:text="O.Oppo"ohos:text_size="14fp"/></RadioContainer></DirectionalLayout>
修改RadionButton的行为
下面的代码在RadioButton的选中状态发生变化时修改按钮是否接受单击的属性。
public class MainAbilitySlice extends AbilitySlice {public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);AbsButton.CheckedStateChangedListener listener = new AbsButton.CheckedStateChangedListener() {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 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
评论
