HarmonyOS 如何全局关闭键盘?

在不调用this.controller.stopEditing();方式的情况下如何收起键盘。

阅读 861
1 个回答

请参考以下demo

import inputMethod from '@ohos.inputMethod';
@Entry
@Component
struct HideKeyboard {
  controller: TextInputController = new TextInputController();
  build() {
    Column() {
      KeyBoard({controller:this.controller})
      Button('关闭')
        .onClick(() => {
          this.controller.stopEditing()
        })
    }
  }
}

@Component
struct KeyBoard{
  controller: TextInputController = new TextInputController();
  @State inputValue: string = '';
  @State show: boolean = false;

  // 自定义键盘组件
  @Builder
  CustomKeyboardBuilder() {
    Column() {
      Grid() {
        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {
          GridItem() {
            Button(item + '')
              .width(110)
              .onClick(() => {
                this.inputValue += item;
              })
          }
        })
      }
      .maxCount(3)
      .columnsGap(10)
      .rowsGap(10)
      .padding(5)
    }
    .backgroundColor(Color.Gray)
  }
  build() {
    TextInput({ controller: this.controller, text: this.inputValue })// 绑定自定义键盘
      .customKeyboard(this.CustomKeyboardBuilder())
      .margin(10)
      .height(48)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进