[@Entry](/user/Entry)
[@Component](/user/Component)
struct Page48 {
[@State](/user/State) text: string = "TTTTT"
[@State](/user/State) index: number = -1
[@State](/user/State) space: number = 8
[@State](/user/State) arrowPosition: ArrowPosition = ArrowPosition.END
[@State](/user/State) arr: ESObject[] = [{ value: 'aaa', icon: $r("app.media.app_icon") },
{ value: 'bbb', icon: $r("app.media.app_icon") },
{ value: 'ccc', icon: $r("app.media.app_icon") },
{ value: 'ddd', icon: $r("app.media.app_icon") }]
build() {
Column() {
Button(‘清空选中的内容’).onClick(() => {
if (this.arr[this.index]) {
this.arr.splice(this.index, 1)
this.text = “”
}
})
Button(‘清空全部内容’).onClick(() => {
this.arr.length = 0
this.text = “”
})
Button(‘添加内容’).onClick(() => {
this.arr.push({ value: ‘ddd’, icon: $r(“app.media.app_icon”) })
})
Select(this.arr)
.selected(this.index)
.value(this.text)
.font({ size: 16, weight: 500 })
.fontColor(’#182431’)
.selectedOptionFont({ size: 16, weight: 400 })
.optionFont({ size: 16, weight: 400 })
.space(this.space)
.arrowPosition(this.arrowPosition)
.menuAlign(MenuAlignType.START, { dx: 0, dy: 0 })
.optionWidth(200)
.optionHeight(300)
.onSelect((index: number, text?: string | undefined) => {
console.info(‘Select:’ + index)
this.index = index;
if (text) {
this.text = text;
}
})
}.width(‘100%’)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
HarmonyOS 鸿蒙Next支持清空选中的内容。你可以通过修改绑定到Select组件的State变量的值来实现这一点。将State变量的值设置为初始值或空值,即可清空Select组件中选中的内容。同时,确保在UI中正确地绑定了这个State变量。如果问题依旧没法解决请加我微信,我的微信是itying888。