【HarmonyOS NEXT】是否有监听键盘显隐的方法

关键字

监听 / 键盘 / 输入法框架 / 窗口

问题描述

是否有监听键盘显隐的方法?

解决方案

可选择以下任一方案:

方案一:

通过输入法框架模块(@ohos.inputMethod)来监听软键盘状态。

用InputMethodController实例的on('sendKeyboardStatus')方法来监听,直接在inputMethodController.on('sendKeyboardStatus', callback)的callback中处理。详细信息请参见:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inputmethod-0000001774281542#ZH-CN_TOPIC_0000001774281542__onsendkeyboardstatus10

方案二

通过窗口模块(@ohos.window)来监听软键盘状态。

用Window实例的on('keyboardHeightChange')方法来监听软键盘高度,可以判断软键盘状态。

详细信息请参见:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-window-0000001820880785#ZH-CN_TOPIC_0000001820880785__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));
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值