HarmonyOS鸿蒙Next中Radio和Checkbox清空选项

HarmonyOS鸿蒙Next中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')
    }.width('50%').height('100%')
      .onClick(() => {
        this.cleanCondition();
      })

    Column({ space: 10 }) {
      Text('查询').fontSize(14)
        .layoutWeight(1)
        .textAlign(TextAlign.Center)
        .fontColor(Color.White)
    }.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() }
}

} }

@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 }) } }


更多关于HarmonyOS鸿蒙Next中Radio和Checkbox清空选项的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

你好,看了下你的demo,了解到你的需求是,清空Radio和Checkbox组件的选中状态,当前这两个组件的选中状态都是可以通过“双向绑定”的方式,进行一键清空的

官网链接: https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-checkbox#select https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-radio#checked

也可以参考下面简单的demo示例:

@State gender: string = '';
@State boyChecked: boolean = false;
@State girlChecked: boolean = false;
@State cb1: boolean = false;
@State cb2: boolean = false;

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

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

      Column() {
        Radio({ value: '女', group: 'radioGender' })
          .checked(this.girlChecked)
          .height(15)
          .width(15)
          .onChange((isChecked: boolean) => {
            this.gender = '女';
          })
      }.width('15')
      Column() {
        Text('女')
      }.width('50')
    }.margin({ left: 20 })

    Flex({ justifyContent: FlexAlign.SpaceEvenly }) {
      Checkbox({ name: 'checkbox1', group: 'checkboxGroup' })
        .select(this.cb1)
      Column() {
        Text('苹果')
      }.width('50')
      Checkbox({ name: 'checkbox2', group: 'checkboxGroup' })
        .select(this.cb2)
      Column() {
        Text('香蕉')
      }.width('50')
    }

    Button('清空')
      .onClick(() => {
        this.boyChecked = false;
        this.girlChecked = false;
        this.cb1 = false;
        this.cb2 = false;
      })
      .margin({ left: 20 })
  }
}

更多关于HarmonyOS鸿蒙Next中Radio和Checkbox清空选项的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


您这确实可以,但在我代码里为什么就没生效呢

@Entry @Component export struct GetConditionPage { @State gender: string = ‘’; @State boyChecked: boolean = false; @State girlChecked: boolean = false; @State sAge: string = ‘’; @State eAge: string = ‘’; @State ageRange: string[] = []; @State degreeList: string[] = []; @State degreeSelected: string[] = []; @State educationList: string[] = []; @State educationSelected: string[] = [];

aboutToAppear(): void { for (let index = 0; index < 65; index++) { this.ageRange.push(index.toString()); } this.degreeList = [‘博士’, ‘硕士’, ‘学士’]; this.educationList = [‘研究生’, ‘大学’, ‘大专’, ‘中专及以下’]; }

build() { Column() { Row({ space: 10 }) { Scroll() { Column() { Row() { Column({ space: 10 }) { Text(‘性别:’).classDes1() }.width(‘10%’) Column({ space: 10 }) { Row({ space: 10 }) { Column() { Radio({ value: ‘男’, group: ‘radioGender’ }) .checked(this.boyChecked) .radioStyle({ checkedBackgroundColor: ‘#00afad’ }) .height(15) .width(15) .onChange((isChecked: boolean) => { this.gender = ‘男’; }) }.width(‘15’) Column() { Text(‘男’).classDes2() }.width(‘70’) Column() { Radio({ value: ‘女’, group: ‘radioGender’ }) .checked(this.girlChecked) .radioStyle({ checkedBackgroundColor: ‘#00afad’ }) .height(15) .width(15) .onChange((isChecked: boolean) => { this.gender = ‘女’; }) }.width(‘15’) Column() { Text(‘女’).classDes2() }.width(‘70’) }.margin({ left: 20 }) }.width(‘90%’) .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Start) }.backgroundColor(’#e7e9e8’).height(40) }.height(‘100%’) }.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')
    }.width('50%').height('100%')
    .onClick(() => {
      this.cleanCondition();
    })

    Column({ space: 10 }) {
      Text('查询').fontSize(14)
        .layoutWeight(1)
        .textAlign(TextAlign.Center)
        .fontColor(Color.White)
    }.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.boyChecked = false; this.girlChecked = false;

this.sAge = '';
this.eAge = '';
this.degreeSelected = [];
this.educationSelected = [];

}

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

@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) }

老师,加了$$this.boyChecked可以变更显示,但查询 cleanCondition() 中this.gender = ‘’;无效了,求助,

解决了,多谢

性别选择器

  • [ ] 男
  • [ ] 女

教育程度选择器

  • [ ] 小学
  • [ ] 中学
  • [ ] 高中
  • [ ] 大学

(注意:上述示例中的教育程度选项如"小学"、"中学"、"高中"、"大学"为假设内容,实际应替换为`educationList`数组中的具体内容。由于HTML代码中并没有提供具体的教育程度选项,所以这里使用了假设的选项。)

在HarmonyOS Next中清空Radio或Checkbox选项:

  1. Radio组件清空:设置RadioGroup的selected属性为-1
radioGroup.selected = -1
  1. Checkbox组件清空:
  • 单个Checkbox:设置select属性为false
checkbox.select = false
  • CheckboxGroup:设置selectAll(false)方法
checkboxGroup.selectAll(false)

在HarmonyOS Next中清空Radio和Checkbox的选中状态,需要同时处理数据状态和UI状态。根据你的代码,以下是关键实现点:

  1. 对于Radio组件:
  • 在cleanCondition()中已经正确清除了gender状态变量
  • 但需要在genderRadio组件中通过.checked()绑定当前选中状态:
Radio({value: '男', group: 'radioGender'})
  .checked(this.gender === '男')
  //...
Radio({value: '女', group: 'radioGender'})
  .checked(this.gender === '女')
  1. 对于Checkbox组件:
  • 已正确清空degreeSelected数组
  • 需要在degreeCheckBox组件中绑定选中状态:
Checkbox({
  name: item.toString(),
  group: 'degree'
})
.selected(this.degreeSelected.includes(item))
  1. 完整cleanCondition()方法:
cleanCondition() {
  this.gender = '';
  this.sAge = '';
  this.eAge = '';
  this.degreeSelected = []; // 清空数组会触发ForEach重新渲染
}

关键是要确保:

  1. 状态变量被正确重置
  2. 组件通过属性绑定实时响应状态变化
  3. 数组类型的选中状态通过includes()方法判断

这种响应式设计会自动更新UI,无需手动操作DOM。

回到顶部