HarmonyOS TextInput在输入状态下,如何让点击界面任何地方,收起键盘?

如题:HarmonyOS TextInput在输入状态下,如何让点击界面任何地方,收起键盘?

阅读 452
1 个回答

请参考以下示例:

import { inputMethod } from '@kit.IMEKit';
@Entry
@Component
struct LeftRightTest {
  @State a:boolean =true;
  controller: TextInputController = new TextInputController()
  build() {
    Column() {
      TextInput({placeholder: '请输入内容'})
        .backgroundColor(Color.Orange)
        .onTouch(()=>{
          this.a=true;
        })
      Button('点我')
      TextInput({placeholder: '请输入内容'})
        .backgroundColor(Color.Orange)
        .onTouch(()=>{
          this.a=true;
        })
    }
    .height('100%')
    .width('100%')
    .onTouch(() => {
      if (this.a!=true) {
        // 收起键盘
        let inputMethodController = inputMethod.getController();
        inputMethodController.stopInputSession()
      }
      this.a=false
    })
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进