HarmonyOS 输入法键盘显隐监听问题:sendKeyboardStatus

在对输入法,软键盘状态进行监听时,sendKeyboardStatus。发现点击键盘自带的下拉按键,导致inputMethodController.on("sendKeyboardStatus")这个监听丢失,不再有显示、隐藏的回调。

复现步骤:(运行下面的示例代码)

1、正常情况:”点击显示输入法键盘“按钮,唤起键盘,toast键盘显示;”点击隐藏输入法键盘“按钮,toast键盘隐藏。可以重复点击多少,每次都有监听回调,没有问题。

2、异常情况:”点击显示输入法键盘“按钮,唤起键盘,toast键盘显示;点击键盘自带的下拉按键(键盘表情旁边的下拉箭头),toast键盘隐藏。之后无论再怎么点击显示或隐藏键盘,都没有toast提示了。疑似sendKeyboardStatus监听被off掉了。请问该如何处理并解决这个问题?或者有没有其他方式监听键盘的显示和隐藏状态。

示例代码如下:

import { inputMethod } from '@kit.IMEKit';
import { promptAction } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct TextInputPage {
  @State isShow: boolean = false;
  inputMethodController = inputMethod.getController();

  aboutToAppear(): void {
    let textConfig: inputMethod.TextConfig = {
      inputAttribute: {
        textInputType: 0,
        enterKeyType: 1
      }
    };
    this.inputMethodController.attach(false, textConfig, (err: BusinessError) => {
      if (err) {
        console.error(`Failed to attach: ${JSON.stringify(err)}`);
        return;
      }
      console.log('Succeeded in attaching the inputMethod.');

      this.inputMethodController.on("sendKeyboardStatus", (keyboardStatus: inputMethod.KeyboardStatus) => {
        if (keyboardStatus === inputMethod.KeyboardStatus.HIDE) {
          console.log('inputMethod.KeyboardStatus.HIDE.');
          promptAction.showToast({ message: "键盘隐藏" })
        } else if (keyboardStatus === inputMethod.KeyboardStatus.SHOW) {
          console.log('inputMethod.KeyboardStatus.SHOW.');
          promptAction.showToast({ message: "键盘显示" })
        }
      })
    });
  }

  build() {
    Stack({ alignContent: Alignment.Bottom }) {
      RelativeContainer() {
        Button("点击隐藏输入法键盘")
          .id('text')
          .alignRules({
            left: { anchor: '__container__', align: HorizontalAlign.Start },
            right: { anchor: '__container__', align: HorizontalAlign.End },
            top: { anchor: '__container__', align: VerticalAlign.Top }
          })
          .onClick(() => {
            this.isShow = false
            this.inputMethodController.hideTextInput()
          })

        Button("点击显示输入法键盘")
          .id('btn')
          .alignRules({
            left: { anchor: '__container__', align: HorizontalAlign.Start },
            right: { anchor: '__container__', align: HorizontalAlign.End },
            bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
          })
          .onClick(() => {
            this.isShow = true
            this.inputMethodController.showTextInput()
          })
      }
      .expandSafeArea([SafeAreaType.KEYBOARD])
      .height('100%')
      .width('100%')

      TextInput({ placeholder: "请输入。。。" })
        .id('TextInputPageHelloWorld')
        .fontSize(15)
        .fontWeight(FontWeight.Bold)
        .height('auto')
        .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.TOP])
        .visibility(this.isShow ? Visibility.Visible : Visibility.None)
    }
    .height('100%')
    .width('100%')

  }
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

可使用window.on(‘keyboardHeightChange’)监听键盘高度,判断软键盘的显示与隐藏。文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#onkeyboardheightchange7

import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct TextAreaExample {
  @State text: string = ''
  @State counterVisible: boolean = false
  @State maxNumber: number = -1
  data:number = 10;
  @State isHideKeybord: boolean = true
  controller: TextAreaController = new TextAreaController()
  aboutToAppear(): void {
    console.log('success')
  }
  onPageShow(): void {
    console.log('success')
    this.isHideKeybord = (this.data > 0)? false : true;
    console.log(JSON.stringify(this.data))
    console.log(JSON.stringify(this.isHideKeybord))
    window.getLastWindow(getContext()).then(lastWindow => {
      lastWindow.on('keyboardHeightChange', (data) => {
        console.log(JSON.stringify(data))
        this.isHideKeybord = (data > 0)? false : true;
      })
    }).catch((err: BusinessError) => {
      console.error('Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(err));
    })
  }

  build() {
    Column() {
      TextArea({
        text: this.text,
        placeholder: '请输入',
        controller: this.controller
      })
        .placeholderFont({ size: 16, weight: 400 })
        .width(336)
        .height(56)
        .margin(20)
        .fontSize(16)
        .fontColor('#182431')
        .maxLength(10)
        .showCounter(true)
        .backgroundColor('#FFFFFF')
        .onChange((value: string) => {
          this.text = value
        })
    }.width('100%')
     .height('100%')
     .backgroundColor('#F1F3F5')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
小程序输入法键盘光标问题
355浏览 • 1回复 待解决
HarmonyOS如何监听输入法显示隐藏
646浏览 • 1回复 待解决
HarmonyOS 自带输入法输入问题
39浏览 • 1回复 待解决
如何监听输入法输入内容的变化?
397浏览 • 1回复 待解决
HarmonyOS 输入法光标控制
70浏览 • 1回复 待解决
输入法编程接口吗?
4735浏览 • 1回复 待解决
HarmonyOS 本地模拟器怎么使用输入法
37浏览 • 1回复 待解决