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 + "被取消选中了");
        }
    }
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeniusAng丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值