组件说明:
父类是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 + "被取消选中了");
}
}
}