HarmonyOS 软键盘弹出的时候要在软键盘上方增加几个业务按钮, 请问非输入法应用怎么监听软键盘的弹起或者自定义键盘弹起时,才展示的view。?

如题:HarmonyOS 软键盘弹出的时候要在软键盘上方增加几个业务按钮, 请问非输入法应用怎么监听软键盘的弹起或者自定义键盘弹起时,才展示的view。?

阅读 508
1 个回答

监听软键盘的可以通过两个方式:通过输入法框架模块来监听(@ohos.inputMethod)用InputMethodController实例的on('sendKeyboardStatus')方法来监听,直接在inputMethodController.on('sendKeyboardStatus', callback)的callback中处理。

通过输入法框架模块来监听软键盘状态参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-inputmethod-V5\#onsendkeyboardstatus10

通过窗口模块来监听(@ohos.window)用Window实例的on('keyboardHeightChange')方法来监听软键盘高度,也能判断软键盘状态的。

通过窗口模块来监听软键盘状态参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#onkeyboardheightchange7

示例代码:

let windowClass: window.Window | undefined = undefined;
try {
  window.getLastWindow(this.context, (err: BusinessError, data) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
      return;
    }
    windowClass = data;
    console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    windowClass.on('avoidAreaChange', (data) => {
      console.info('Succeeded in enabling the listener for system avoid area changes. type:' +
      JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));
    });
  });
} catch (exception) {
  console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进