IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> HarmonyOS开发38:RadioButton单选框基本用法 -> 正文阅读

[系统运维]HarmonyOS开发38:RadioButton单选框基本用法

组件说明:

父类是AbsButton,而AbsButton的父类是Button。在使用的时候需要用到单选按钮的按钮组。
RadioContainer,在一组内多选按钮只能选择其中一个。

当需要监听单选框的状态时,不要用AbsButton里面的CheckedStateChangedListener。而是给按钮组
RadioContainer添加事件。用RadioContainer里面的CheckedStateChangedListener。

常见属性:

属性名称功能说明
marked单选按钮的选中状态。true为选中,false为没有选中。
check_element自定义选择框的样式。样式需要跟marked的值对应。

常见方法:

方法名称功能说明
setChecked设置多选框的选中状态。true为选中,false为没有选中。
isChecked判断多选框的选中状态。true为选中,false为没有选中。
setCheckedStateChangedListener添加一个状态监听事件(一般不用)

按钮组RadioContainer常见方法:

方法名称功能说明
setMarkChangedListener添加状态监听事件,可以监听按钮组里面单选按钮的状态是否改变

同上篇文章讲到的一样,本篇文章也需要用到面写的吐司弹框,记得提前加入到项目当中

ability_main.xml代码:

<?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:alignment="center"
    ohos:orientation="vertical">

    <RadioContainer
        ohos:id="$+id:rc"
        ohos:height="match_content"
        ohos:width="match_content">

        <RadioButton
            ohos:id="$+id:boy"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="#21A8FD"
            ohos:text=""
            ohos:text_alignment="center"
            ohos:text_size="30fp"/>

        <RadioButton
            ohos:id="$+id:girl"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="#21A8FD"
            ohos:text=""
            ohos:text_alignment="center"
            ohos:text_size="30fp"
            ohos:top_margin="10vp"/>

    </RadioContainer>

<!--    <RadioContainer
        ohos:height="match_content"
        ohos:width="match_content">

        <RadioButton
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="#21A8FD"
            ohos:text="篮球"
            ohos:text_alignment="center"
            ohos:text_size="30fp"
            ohos:top_margin="10vp"/>

        <RadioButton
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="#21A8FD"
            ohos:text="足球"
            ohos:text_alignment="center"
            ohos:text_size="30fp"
            ohos:top_margin="10vp"/>

        <RadioButton
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="#21A8FD"
            ohos:text="排球"
            ohos:text_alignment="center"
            ohos:text_size="30fp"
            ohos:top_margin="10vp"/>

    </RadioContainer>-->

</DirectionalLayout>

MainAbilitySlice.java代码:

package com.example.radiobuttonapplication.slice;

import com.example.radiobuttonapplication.ResourceTable;
import com.example.radiobuttonapplication.toastUtils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.RadioButton;
import ohos.agp.components.RadioContainer;

public class MainAbilitySlice extends AbilitySlice implements RadioContainer.CheckedStateChangedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到组件
        RadioButton boy = (RadioButton) findComponentById(ResourceTable.Id_boy);
        RadioButton girl = (RadioButton) findComponentById(ResourceTable.Id_girl);
        RadioContainer rc = (RadioContainer) findComponentById(ResourceTable.Id_rc);

        //boy.setChecked(true);
        //boy.setCheckedStateChangedListener(this);

        rc.setMarkChangedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    //参数一:单选框按钮组的对象
    //参数二:索引,表示按钮组中第i个按钮
    @Override
    public void onCheckedChanged(RadioContainer radioContainer, int i) {
        RadioButton rb = (RadioButton) radioContainer.getComponentAt(i);
        String text = rb.getText();
        if (rb.isChecked()) {
            ToastUtils.showDialog(this, text + "被选中了");
        } else {
            ToastUtils.showDialog(this, text + "被取消选中了");
        }
    }
}

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 17:05:23  更:2021-08-12 17:07:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 15:50:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码