文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS Radio和Checkbox清空选项问题(API12+)

如何实现通过cleanCondition()方法,清空界面上Radio和Checkbox已选选项,实现渲染成未选状态?

代码如下:

收起
自动换行
深色代码主题
复制
@Entry
@Component
export struct GetConditionPage {
  @State gender: string = '';
  @State sAge: string = '';
  @State eAge: string = '';
  @State ageRange: string[] = [];
  @State degreeList: string[] = [];
  @State degreeSelected: string[] = [];

  aboutToAppear(): void {
    for (let index = 0; index < 65; index++) {
      this.ageRange.push(index.toString());
    }
    this.degreeList = ['博士', '硕士', '学士'];
  }

  build() {
    Column() {
      Row({ space: 10 }) {
        Scroll() {
          Column() {
            Row() {
              Column({ space: 10 }) {
                Text('性别:').classDes1()
              }.width('10%')

              Column({ space: 10 }) {
                genderRadio({ gender: $gender })
              }.width('90%')
              .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Start)
            }.backgroundColor('#e7e9e8').height(40)

            Row() {
              Column({ space: 10 }) {
                Text('年龄:').classDes1()
              }.width('10%')

              Column({ space: 10 }) {
                ageRange({ sAge: $sAge, eAge: $eAge, ageRange: $ageRange })
              }.width('90%')
              .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Start)
            }.height(40)

            Row() {
              Column({ space: 10 }) {
                Text('全日制学位:').classDes1()
              }.width('10%')

              Column({ space: 10 }) {
                degreeCheckBox({ degreeList: $degreeList, degreeSelected: $degreeSelected })
              }.width('90%')
              .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Start)
            }.backgroundColor('#e7e9e8').height(40)
          }
        }
        .width('100%')
        .height('100%')
        .scrollable(ScrollDirection.Vertical) // 滚动方向为垂直方向
        .scrollBar(BarState.Auto) // 滚动条常驻显示
        .scrollBarColor(Color.Gray) // 滚动条颜色
        .edgeEffect(EdgeEffect.Fade)
      }.width('100%').height('92%').backgroundColor(Color.White)

      Divider().strokeWidth(2) // 设置横线的宽度
      Row({ space: 10 }) {
        Column({ space: 10 }) {
          Text('清空').fontSize(14)
            .layoutWeight(1)
            .textAlign(TextAlign.Center)
            .fontColor('#006F6A')
          // .fontWeight(FontWeight.Bolder)
        }.width('50%').height('100%')
        .onClick(() => {
          this.cleanCondition();
        })

        Column({ space: 10 }) {
          Text('查询').fontSize(14)
            .layoutWeight(1)
            .textAlign(TextAlign.Center)
            .fontColor(Color.White)
          // .fontWeight(FontWeight.Bolder)
        }.width('50%').height('100%')
        .backgroundColor('#00afad')
        .onClick(() => {
          this.getConditionEmpList();
        })
      }.width('100%').height('8%').backgroundColor(Color.White)
    }
    .backgroundColor(Color.Transparent)
    .alignItems(HorizontalAlign.Center)
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)
  }

  cleanCondition() {
    this.gender = '';
    this.sAge = '';
    this.eAge = '';
    this.degreeSelected = [];
  }

  getConditionEmpList() {
    console.log('性别:' + this.gender)
    console.log('年龄开始:' + this.sAge)
    console.log('年龄结束:' + this.eAge)
    console.log('学位:' + JSON.stringify(this.degreeSelected))
  }
}

@Extend(Text)
function classDes1() {
  .fontSize(14)
  .fontWeight(FontWeight.Normal)
  .fontColor("#000000")
  .width('100%')
  .height('100%')
  .textAlign(TextAlign.End)
}

@Extend(Text)
function classDes2() {
  .fontSize(14)
  .fontWeight(FontWeight.Normal)
  .fontColor("#000000")
  .width('100%')
  .height('100%')
  .textAlign(TextAlign.Start)
}

@Component
struct genderRadio {
  @Link gender: string;

  build() {
    Row({ space: 10 }) {
      Column() {
        Radio({ value: '男', group: 'radioGender' })
          .checked(false)
          .radioStyle({
            checkedBackgroundColor: '#00afad'
          })
          .height(15)
          .width(15)
          .onChange((isChecked: boolean) => {
            this.gender = '男';
          })
      }.width('15')

      Column() {
        Text('男').classDes2()
      }.width('50')

      Column() {
        Radio({ value: '女', group: 'radioGender' })
          .checked(false)
          .radioStyle({
            checkedBackgroundColor: '#00afad'
          })
          .height(15)
          .width(15)
          .onChange((isChecked: boolean) => {
            this.gender = '女';
          })
      }.width('15')

      Column() {
        Text('女').classDes2()
      }.width('50')
    }.margin({ left: 20 })
  }
}

@Component
struct ageRange {
  @Link sAge: string;
  @Link eAge: string;
  @Link ageRange: string[];

  build() {
    Row({ space: 10 }) {
      Column() {
        Button(this.sAge.toString())
          .width('70')
          .height('30')
          .backgroundColor('#c9ccd0')
          .fontColor(Color.Black)
          .fontSize(14)
          .onClick(() => {
            TextPickerDialog.show({
              range: this.ageRange,
              value: this.sAge,
              onAccept: (result) => {
                if (result.value.toString() === '0') {
                  this.sAge = ''
                } else {
                  this.sAge = result.value.toString()
                }
              }
            })
          })
      }.width('100')

      Column() {
        Text('岁(含)到').classDes2()
      }.width('70')

      Column() {
        Button(this.eAge)
          .width('70')
          .height('30')
          .backgroundColor('#c9ccd0')
          .fontColor(Color.Black)
          .fontSize(14)
          .onClick(() => {
            TextPickerDialog.show({
              range: this.ageRange,
              value: this.eAge,
              onAccept: (result) => {
                if (result.value.toString() === '0') {
                  this.eAge = ''
                } else {
                  this.eAge = result.value.toString()
                }
              }
            })
          })
      }.width('100')
      Column() {
        Text('岁').classDes2()
      }
      Blank()
    }
  }
}

@Component
struct degreeCheckBox {
  @Link degreeSelected: string[];
  @Link degreeList: string[];

  build() {
    Row({ space: 10 }) {
      ForEach(this.degreeList, (item: string) => {
        Column() {
          Checkbox({
            name: item.toString(), //拿到的数据赋值给name
            group: 'degree'//群组名称
          }).width('15')
            .selectedColor('#00afad')
            .shape(CheckBoxShape.CIRCLE)
            .onChange((value: boolean) => {
              if (value) {
                // 如果多选框被选中,将该选项添加到选中列表中
                if (!this.degreeSelected.includes(item)) {
                  this.degreeSelected.push(item);
                }
              } else {
                // 如果多选框被取消选中,将该选项从选中列表中移除
                const index = this.degreeSelected.indexOf(item);
                if (index > -1) {
                  this.degreeSelected.splice(index, 1);
                }
              }
            })
        }.width('15')

        Column() {
          Text(item).classDes2()
        }.width('50')
      })
    }.margin({ left: 20 })
  }
}
点赞
收藏
回复
1
分享
举报
浏览31 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

清空Radio和Checkbox组件的选中状态,当前这两个组件的选中状态都是可以通过"双向绑定"的方式,进行一键清空的。

参考文档:select、checked。

1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。