HarmonyOS 如何全局关闭键盘

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

HarmonyOS
2024-12-24 15:25:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

请参考以下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)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
分享
微博
QQ
微信
回复
2024-12-24 18:44:45
相关问题
HarmonyOS 如何关闭键盘关闭弹窗
1290浏览 • 1回复 待解决
全局关闭弹窗如何实现?
1326浏览 • 2回复 待解决
HarmonyOS 键盘关闭问题
489浏览 • 1回复 待解决
HarmonyOS键盘如何主动关闭
618浏览 • 1回复 待解决
怎么主动关闭键盘,有人知道吗?
1085浏览 • 1回复 待解决
怎么监听键盘的弹起和关闭事件?
3563浏览 • 1回复 待解决
HarmonyOS如何收起键盘
860浏览 • 1回复 待解决