目录

如何实现TextArea、TextInput获取光标时不拉起键盘

问题现象

TextArea获取光标时会拉起键盘,挡住应用操作界面,如何实现TextArea获取光标时不拉起键盘?

背景知识

解决方案

  1. 创建自定义键盘,并设置其宽高都为0。
  2. 通过customKeyboard属性绑定该自定义键盘。

完整示例参考如下:

@Entry
@Component
struct TextAreaExample {
  controller: TextAreaController = new TextAreaController();
  @State inputValue: string = '';

  // 自定义键盘组件
  @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;
            });
          };
        });
      }
      .height(0)
      .width(0);
    }.backgroundColor(Color.Gray);
  }

  build() {
    Column() {
      TextArea({ controller: this.controller, text: this.inputValue })
        .customKeyboard(this.CustomKeyboardBuilder())
        .margin(10)
        .border({ width: 1 })
        .height(200);
    };
  }
}