当组件处于获焦状态时,将其focusable属性或enabled属性设置为false,会自动使该组件失焦。焦点将按照走焦规则转移给其他组件。参考代码如下:
@Entry
@Component
struct ClearComponentFocus {
// Whether textInput is focus
@State textFocusable: boolean = true;
@State text: string = 'Gain focus';
build() {
Column() {
TextInput({ text: this.text })
.focusable(this.textFocusable)
.onFocus(() => {
this.text = 'Gain focus';
})
.onBlur(() => {
this.text = 'Lost Focus';
})
Button('Button1')
.width(160)
.height(70)
.margin({ top: 20 })
.onClick(() => {
this.textFocusable = !this.textFocusable;
})
}
.width('100%')
.height('100%')
}
} 参考链接