3.5【HarmonyOS鸿蒙开发】组件RadioButton和RadioContainer

作者:韩茹

公司:程序咖(北京)科技有限公司

鸿蒙巴士专栏作家

一、RadioButton

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

1.1、支持的XML属性

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

RadioButton的自有XML属性见下表:

属性名称 中文描述 取值 取值说明 使用案例
marked 当前状态 boolean类型 可以直接设置true/false,也可以引用boolean资源。 ohos:marked=“true"
ohos:marked=”$boolean:true"
text_color_on 处于选中状态的文本颜色 color类型 可以直接设置色值,也可以引用color资源。 ohos:text_color_on="#FFFFFFFF"
ohos:text_color_on="$color:black"
text_color_off 处于未选中状态的文本颜色 color类型 可以直接设置色值,也可以引用color资源。 ohos:text_color_off="#FFFFFFFF"
ohos:text_color_off="$color:black"
check_element 状态标志样式 Element类型 可直接配置色值,也可引用color资源或引用media/graphic下的图片资源。 ohos:check_element="#000000"
ohos:check_element="$color:black
ohos:check_element=”$media:media_src
ohos:check_element=”$graphic:graphic_src"

1.2、创建RadioButton

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

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"ohos:background_element="#33666600"ohos:padding="20vp"><RadioButtonohos:id="$+id:rb_1"ohos:height="40vp"ohos:width="match_content"ohos:text="A.程序咖"ohos:text_size="20fp"/></DirectionalLayout>

效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C408zrsa-1625447087352)(https://img.chengxuka.com/radiobuttonyunxing1.gif)]

1.3、设置RadioButton

设置单选按钮的字体颜色:

在xml中设置:text_color_on为选中状态的字体颜色,text_color_off为未选中状态的字体颜色。

<Imageohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="center"ohos:top_margin="20vp"ohos:image_src="$media:chengxuka"ohos:alpha="0.5"/>

设置单选按钮字体颜色效果:

也可以在Java代码中设置:

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

二、RadioContainer

RadioContainer是RadioButton的容器,在其包裹下的RadioButton保证只有一个被选项。

2.1 支持的XML属性

RadioContainer的共有XML属性继承自:DirectionalLayout

2.2 创建RadioContainer

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

<!--RadioContainer是RadioButton的容器,在其包裹下的RadioButton保证只有一个被选项。--><Textohos:height="match_content"ohos:width="match_parent"ohos:text="请问以下选项中哪一个是女孩子?"ohos:text_size="18fp"ohos:padding="20vp"ohos:background_element="#33ff0000"/><RadioContainerohos:id="$+id:radio_container"ohos:height="match_content"ohos:width="match_parent"ohos:left_padding="20vp"ohos:bottom_padding="20vp"ohos:right_padding="20vp"ohos:background_element="#33ff0000"><!-- 放置多个RadioButton --><RadioButtonohos:id="$+id:radio_button_1"ohos:height="40vp"ohos:width="match_content"ohos:text="A.漩涡鸣人"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_2"ohos:height="40vp"ohos:width="match_content"ohos:text="B.宇智波佐助"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_3"ohos:height="40vp"ohos:width="match_content"ohos:text="C.我爱罗"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_4"ohos:height="40vp"ohos:width="match_content"ohos:text="D.日向雏田"ohos:text_size="18fp"/></RadioContainer>

效果:

我们也可以设置RadioButton的布局方向:orientation设置为“horizontal”,表示横向布局;orientation设置为“vertical”,表示纵向布局。默认为纵向布局。

在xml中设置:

<RadioContainer...ohos:orientation="horizontal">...
</RadioContainer>

或者在Java代码中设置:

container.setOrientation(Component.HORIZONTAL);

示例代码:

<Textohos:top_margin="20vp"ohos:height="match_content"ohos:width="match_parent"ohos:text="请问你的手机是哪种品牌?"ohos:text_size="18fp"ohos:padding="20vp"ohos:background_element="#33ff0000"/><RadioContainerohos:id="$+id:radio_container2"ohos:height="match_content"ohos:width="match_parent"ohos:left_padding="20vp"ohos:bottom_padding="20vp"ohos:right_padding="20vp"ohos:orientation="horizontal"ohos:background_element="#33ff0000"><!-- 放置多个RadioButton --><RadioButtonohos:id="$+id:radio_button_5"ohos:height="40vp"ohos:width="match_content"ohos:text="A.华为"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_6"ohos:height="40vp"ohos:width="match_content"ohos:text="B.水果"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_7"ohos:height="40vp"ohos:width="match_content"ohos:text="C.小米"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_8"ohos:height="40vp"ohos:width="match_content"ohos:text="D.魅族"ohos:text_size="18fp"/></RadioContainer>

效果图:

2.3 设置RadioContainer

设置响应RadioContainer状态改变的事件。

RadioContainer container = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);
container.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {@Overridepublic void onCheckedChanged(RadioContainer radioContainer, int index) {}
});

根据索引值设置指定RadioButton为选定状态。

container.mark(0);

清除RadioContainer中所有RadioButton的选定状态。

container.cancelMarks();

三、写个例子

我们设置了3个选择题,每一道题都设置不同的处理方式。

第一道题,当某个单选按钮被选中时,我们在屏幕中间弹出ToastDialog吐司对话框。

第二道题,当某个单选按钮被选中时,我们更改被选中的单选按钮的颜色。

第三道题,我们获取被选中的单选按钮的文本内容,显示在下面的Text中。

首先我们先完成layout目录下的xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"ohos:background_element="#33666600"ohos:padding="20vp"><!--RadioContainer是RadioButton的容器,在其包裹下的RadioButton保证只有一个被选项。--><!--第一题:--><Textohos:height="match_content"ohos:width="match_parent"ohos:text="请问以下选项中哪一个是女孩子?"ohos:text_size="18fp"ohos:padding="20vp"ohos:background_element="#33ff0000"/><RadioContainerohos:id="$+id:radio_container"ohos:height="match_content"ohos:width="match_parent"ohos:left_padding="20vp"ohos:bottom_padding="20vp"ohos:right_padding="20vp"ohos:background_element="#33ff0000"><!-- 放置多个RadioButton --><RadioButtonohos:id="$+id:radio_button_1"ohos:height="40vp"ohos:width="match_content"ohos:text="A.漩涡鸣人"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_2"ohos:height="40vp"ohos:width="match_content"ohos:text="B.宇智波佐助"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_3"ohos:height="40vp"ohos:width="match_content"ohos:text="C.我爱罗"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_4"ohos:height="40vp"ohos:width="match_content"ohos:text="D.日向雏田"ohos:text_size="18fp"/></RadioContainer><!--    第二题:xml中我们设置了每个RadioButton的选中状态的颜色为红色--><Textohos:top_margin="20vp"ohos:height="match_content"ohos:width="match_parent"ohos:text="请问你的手机是哪种品牌?"ohos:text_size="18fp"ohos:padding="20vp"ohos:background_element="#33ff0000"/><RadioContainerohos:id="$+id:radio_container2"ohos:height="match_content"ohos:width="match_parent"ohos:left_padding="20vp"ohos:bottom_padding="20vp"ohos:right_padding="20vp"ohos:orientation="horizontal"ohos:background_element="#33ff0000"><!-- 放置多个RadioButton --><RadioButtonohos:id="$+id:radio_button_5"ohos:height="40vp"ohos:width="match_content"ohos:text="A.华为"ohos:text_color_on="#FF0000"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_6"ohos:height="40vp"ohos:width="match_content"ohos:text_color_on="#FF0000"ohos:text="B.水果"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_7"ohos:height="40vp"ohos:width="match_content"ohos:text_color_on="#FF0000"ohos:text="C.小米"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_8"ohos:height="40vp"ohos:width="match_content"ohos:text_color_on="#FF0000"ohos:text="D.魅族"ohos:text_size="18fp"/></RadioContainer><!--    第三题:--><Textohos:top_margin="20vp"ohos:height="match_content"ohos:width="match_parent"ohos:text="请问你的性别?"ohos:text_size="18fp"ohos:padding="20vp"ohos:background_element="#33ff0000"/><RadioContainerohos:id="$+id:radio_container3"ohos:height="match_content"ohos:width="match_parent"ohos:left_padding="20vp"ohos:bottom_padding="20vp"ohos:right_padding="20vp"ohos:orientation="horizontal"ohos:background_element="#33ff0000"><!-- 放置多个RadioButton --><RadioButtonohos:id="$+id:radio_button_9"ohos:height="40vp"ohos:width="match_content"ohos:text="A.男性"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_10"ohos:height="40vp"ohos:width="match_content"ohos:text="B.女性"ohos:text_size="18fp"/><RadioButtonohos:id="$+id:radio_button_11"ohos:height="40vp"ohos:width="match_content"ohos:text="C.其他"ohos:text_size="18fp"/></RadioContainer><Textohos:id="$+id:text_checked"ohos:height="match_content"ohos:width="match_content"ohos:text_size="20fp"ohos:top_margin="20vp"ohos:text="[你的性别:]"ohos:text_color="#FF3333"/></DirectionalLayout>

然后在MainAbilitySlice.java中,我们在onStart()方法中,我们添加代码实现,先来第一题,说一下思路:

第一题思路:我们打算当某个RadioButton被选中的时候,在屏幕中间弹出吐司ToastDialog,显示的内容为被选中的选项文本以及正确或者错误的答案。
step1,我们先根据监听事件中的index参数,获取被选中的RadioButton对象
step2,获取该对象的文本内容
step3,判断正确
step4,创建ToastDialog对象,并show()显示出来。

示例代码:

//第一题:设置吐司//1.获取RadioContainer对象RadioContainer container = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);//2.设置响应RadioContainer状态改变的事件。container.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {/**** @param radioContainer,单选按钮组对象* @param index,被选中的RadioButton的下标序号,从0开始*/@Overridepublic void onCheckedChanged(RadioContainer radioContainer, int index) {//根据被选中的RadioButton的下标序号,获取RadioButton对象RadioButton radioButton = (RadioButton)radioContainer.getComponentAt(index);//获取文本内容String text = radioButton.getText();String result = index == 3?"正确":"错误";//设置Toast吐司new ToastDialog((getContext())).setText(text+",答案:"+result).setAlignment(LayoutAlignment.CENTER).show();}});

运行效果:

第二题,我们先说一下思路:

第二题,当选中某个RadioButton时,改变被选中的RadioButton的颜色为红色。
有一点我们先说明一下:在xml布局中,我们是可以通过以下两个属性来设置某个RadioButton选中和没有选中的颜色的。ohos:text_color_on=""ohos:text_color_off=""
但是这个只是针对文本颜色,单选按钮前面的小圆点,没有选中的时候默认是白色,选中了默认是黑色。我们这里希望它变成红色,那文本颜色通过text_color_on属性即可实现。代码中要处理的就是被选中时,小圆点变成红色。
step1,我们现在xml布局文件中的每个RadioButton中,设置被选中的文本颜色为红色:ohos:text_color_on="#FF0000"
step2,借助于ShapeElement类,它提供具有颜色渐变的元素实例,该渐变通常用于组件背景。
step3,借助于StateElement类,它提供可以根据组件状态更改的元素对象。

Java代码实现,首先我们先创建一个函数用于创建StateElement对象。该对象有两种状态,选中和没被选中的颜色是不一样的。示例代码:

     //通过以下方法,定义RadioButton的选中和非选中的小圆点的背景色。选中为红色,否则为白色。private StateElement createStateElement(){//提供具有颜色渐变的元素实例,该渐变通常用于组件背景。ShapeElement elementButtonOn = new ShapeElement();//设置选中状态的小圆点为红色elementButtonOn.setRgbColor(RgbPalette.RED);//设置形状,椭圆elementButtonOn.setShape(ShapeElement.OVAL);ShapeElement elementButtonOff = new ShapeElement();elementButtonOff.setRgbColor(RgbPalette.WHITE);elementButtonOff.setShape(ShapeElement.OVAL);//提供可以根据组件状态更改的元素对象。StateElement checkElement = new StateElement();checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED},elementButtonOn);checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},elementButtonOff);return checkElement;}

然后处理RadioContainer,先获取里面所有的RadioButton,然后为每个RadioButton设置元素对象。

             //第二题,设置选中单选按钮的状态//1.获取RadioContainer对象RadioContainer container2 = (RadioContainer) findComponentById(ResourceTable.Id_radio_container2);//2.设置被选中的radioButton为红色,其他的为白色//先获取radiobutton的个数int count = container2.getChildCount();//循环for(int i=0;i<count;i++){//获取每个radiobuttonRadioButton radioButton = (RadioButton)container2.getComponentAt(i);//为光标所在的气泡设置元素对象。radioButton.setButtonElement(createStateElement());}

运行效果:

第三题,还是先来说一下思路,

第三题,当选中某个RadioButton时,我们希望能够取出它的文本内容,显示到某个Text中。这个和第一题到思路有点像,只不过第一题是将文本信息通过ToastDialog的形式展现,而第三题是设置到Text上。
step1,我们先根据监听事件中的index参数,获取被选中的RadioButton对象
step2,获取该对象的文本内容
step3,将它显示到Text上。

Java代码部分:

 //第三题,设置文本//1.获取RadioContainer对象和Text对象RadioContainer container3 = (RadioContainer) findComponentById(ResourceTable.Id_radio_container3);Text text_checked = (Text) findComponentById(ResourceTable.Id_text_checked);//2.获取被选中的radio的文本内容container3.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {@Overridepublic void onCheckedChanged(RadioContainer radioContainer, int index) {//根据被选中的RadioButton的下标序号,获取RadioButton对象RadioButton radioButton = (RadioButton)radioContainer.getComponentAt(index);//获取文本内容String msg = "[你的性别:";String text = radioButton.getText();msg += text+"]";switch (index){case 0:msg += ",呀,是个汉子";break;case 1:msg += ",呀,是个女汉子";break;case 2:msg += ",呃,人妖嚒。。。";break;}text_checked.setText(msg);}});

运行效果:

更多内容:

1、社区:鸿蒙巴士https://www.harmonybus.net/

2、公众号:HarmonyBus

3、技术交流QQ群:714518656

4、视频课:https://www.chengxuka.com

3.5【HarmonyOS鸿蒙开发】组件RadioButton和RadioContainer相关推荐

  1. 基于小熊派的HarmonyOS鸿蒙开发教程——内核篇

    复习时间:貌似很遥远呀!(未定期) 基于小熊派的鸿蒙开发内核篇 一.CMSIS-RTOS2接口 二.HarmonyOS内核开发-任务管理 三.HarmonyOS内核开发-定时器管理 定时器基本概念 定 ...

  2. HarmonyOS鸿蒙开发入门 :鸿蒙系统介绍

    系统定位 HarmonyOS是一款面向万物互联时代的.全新的分布式操作系统. 在传统的单设备系统能力基础上,HarmonyOS提出了基于同一套系统能力.适配多种终端形态的分布式理念,能够支持手机.平板 ...

  3. HVD Manager 无法通过验证该如何解决 (HarmonyOS鸿蒙开发基础知识)

    实战问题 HVD Manager 无法通过验证该如何解决? 解决方案 1.应该是浏览器问题,建议使用IE浏览器打开 2.更换网络试试,通常通过4G或5G热点可以解决 未经授权禁止转载 更多技术交流请加 ...

  4. 3.1【HarmonyOS鸿蒙开发】组件Text

    作者:韩茹 公司:程序咖(北京)科技有限公司 鸿蒙巴士专栏作家 Text是用来显示字符串的组件,在界面上显示为一块文本区域.Text作为一个基本组件,有很多扩展,常见的有按钮组件Button,文本编辑 ...

  5. 2.4【HarmonyOS鸿蒙开发】堆栈布局StackLayout

    作者:韩茹 公司:程序咖(北京)科技有限公司 鸿蒙巴士专栏作家 StackLayout直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左 ...

  6. (0.2)HarmonyOS鸿蒙开发工具DevEco Studio工程文件目录结构

    1.基本不会去动添加修改的一些文件夹 2.主要直接使用操作的文件夹entry 3.开发操作main文件夹 也就是说,我们其实主要就只要用到java,resources两个文件夹和config.json ...

  7. (0.3)HarmonyOS鸿蒙开发工具DevEco Studio 模拟器使用

    1.登录华为账号 2.打开模拟器 3.选择模拟器 4.将程序运行在模拟器 5.当下面的building进度条完成后

  8. (1.3)HarmonyOS鸿蒙启动程序运行流程

    程序启动运行流程: ①解析config.json文件 ②初始化 ③获取入口Ability的全类名(config.json里的module里的mainAbility) ④找到Ability并运行 ⑤运行 ...

  9. 什么都学一点系列之鸿蒙开发Java版简易备忘录

    本文地址:https://blog.csdn.net/qq_40785165/article/details/123908357,转载请附上此链接 .生活不会惯着你,想要不被抛弃,必须自己争气. 大家 ...

最新文章

  1. ACMNO.16用迭代法求 。求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001。 输出保留3位小数 输入 X 输出 X的
  2. 如何定制视频业务- ramdisk 内存文件系统
  3. linux异机拷贝,rman恢复异机数据库
  4. 重庆工商大学c语言实验一答案,重庆工商大学嵌入式实验考试题目答案 - 副本.doc...
  5. 轻松搞定RocketMQ入门
  6. UE4 Run On Server与Run on owning client
  7. springmvc拦截器对请求参数解密_SpringMVC拦截器如何修改请求参数
  8. django的url控制系统
  9. centos7 docker安装_教你如何在 CentOS 7 下 yum 方式安装 Docker 环境
  10. 小程序-涂鸦画笔(案例-集福)
  11. 初级软考程序员不会c语言,软考程序员初级通过率是多少_软考程序员初级多少分过_上学吧...
  12. 【离散数学】图论 第七章(6) 图的结点着色和Welch Powell法、平面图着色、希伍德五色定理、四色定理
  13. SpringBoot+Vue项目外卖点餐系统
  14. Winedit修改快捷键
  15. Linux环境下——C语言聊天室项目
  16. python银行利息问题_(完整版)存款利率问题应用题
  17. win7下安装centOS7双系统
  18. 保姆级在自己电脑搭建我的世界服务器教程
  19. 如何将win10电脑主题设置成深色
  20. 如何打造良好的技术团队分享氛围

热门文章

  1. 美和易思:多效举措众人帮扶,全力以赴稳定就业
  2. Ubuntu系统下文件带小锁如何解决
  3. Radio Link Failure and Recovery
  4. 用计算机弹坚雷抒情版,『明日方舟』有坚雷就不用换雷蛇了吗?(浅谈雷蛇强度)...
  5. 最全的搭建Java的开发环境以及eclipse安装配置开发工具
  6. Maven项目在test文件夹下运行测试类出现 Class Not Found
  7. linux如何root执行run文件,linux执行run文件显示cannot execute binary file
  8. 第八届蓝桥杯单片机省赛----程序题
  9. Java CompletionStage和CompletableFuture
  10. OpenSSL 编程 二:搭建 CA