【鸿蒙 HarmonyOS】UI 组件 ( 单选按钮 | RadioButton 与 RadioContainer 组件 )
文章目录
一、RadioButton 与 RadioContainer 组件
RadioButton 组件就是单选按钮 ;
给出
个 RadioButton 按钮 , 使用 RadioContainer 编组后 , 只能
选
, 同一时刻 , 只能有单个按钮处于选中状态 ;
RadioContainer 组件是单选按钮的编组组件 , 可以将若干 RadioButton 放到 RadioContainer 标签中 , 这些 RadioButton 组件只能有一个处于选中状态 ;
RadioContainer 编组 RadioButton 布局代码示例 :
<?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:radioContainer"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<RadioButton
ohos:id="$+id:radioButton1"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 1"
ohos:text_size="100"/>
<RadioButton
ohos:id="$+id:radioButton2"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 2"
ohos:text_size="100"/>
<RadioButton
ohos:id="$+id:radioButton3"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 3"
ohos:text_size="100"/>
</RadioContainer>
</DirectionalLayout>
显示样式 : 当前单选按钮 2 处于选中状态 ;
下图是使用远程鸿蒙模拟器显示单选按钮 ;
二、监听 RadioContainer 选择事件
调用 RadioContainer 对象的 setMarkChangedListener 方法 , 给 RadioContainer 添加 RadioContainer.CheckedStateChangedListener 监听器 , 可以监听编组在 RadioContainer 下的所有 RadioButton 的选择事件 ;
当某个 RadioButton 被点击时 , 会回调 RadioContainer.CheckedStateChangedListener 监听器的 onCheckedChanged 方法 , 在该方法的第二个参数 int i , 就是被点击的 RadioBtton 的索引 ;
代码示例 :
package com.example.radiobutton.slice;
import com.example.radiobutton.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 获取文本组件
Text text = (Text) findComponentById(ResourceTable.Id_text);
// 获取 RadioContainer
RadioContainer radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer);
radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
@Override
public void onCheckedChanged(RadioContainer radioContainer, int i) {
text.setText("当前选中 : " + i);
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
布局文件 :
<?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:radioContainer"
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="vertical">
<RadioButton
ohos:id="$+id:radioButton1"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 0"
ohos:text_size="100"/>
<RadioButton
ohos:id="$+id:radioButton2"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 1"
ohos:text_size="100"/>
<RadioButton
ohos:id="$+id:radioButton3"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="单选按钮 2"
ohos:text_size="100"/>
</RadioContainer>
<Text
ohos:id="$+id:text"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="当前选中 : "
ohos:text_size="100"
ohos:text_color="#FF0000"/>
</DirectionalLayout>
执行结果 :
三、GitHub 地址
GitHub 主应用 : https://github.com/han1202012/HarmonyHelloWorld
RadioButton 与 RadioContainer 组件示例 Module : https://github.com/han1202012/HarmonyHelloWorld/tree/master/radiobutton
相关文章
- react中类组件传值,函数组件传值:父子组件传值、非父子组件传值[通俗易懂]
- vue页面刷新_vue强制重置组件
- Vue组件封装的过程[通俗易懂]
- golang源码分析:grpc 链接池(5)自定义组件和框架交互流程
- fastadmin 动态下拉组件 SelectPage
- Jmeter系统入门教程(安装、组件使用、Demo展示、连接数据库、压测报告)
- 【鸿蒙 HarmonyOS】UI 组件 ( 拖动条 Slider 组件 )
- 手把手教程|如何开发一个小程序组件
- 件Linux安装附加组件实现更多功能(linux附加组)
- 在ASP中用组件检测当前网卡地址的代码
- PHPGD图像处理组件的常用函数总结
- JS图片浏览组件PhotoLook的公开属性方法介绍和进阶实例代码