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未知归属地
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
了解社区公约,与您携手共创和谐专业的开发者社区。





































苏公安网备 32011402010933号