HarmonyOS 获取的键盘高度比原来的高了?

最新的DEV ECO模拟器键盘比原来获取的高度要高

阅读 696
1 个回答

可以使用窗口相关接口获取软键盘高度,代码如下:

aboutToAppear() {
  window.getLastWindow(getContext(this))
    .then(currentWindow => {
      //获取windowClass对象
      let property = currentWindow.getWindowProperties(); //方式1:获取规避区域
      let avoidArea = currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD);
      // 初始化显示区域高度
      this.screenHeight = px2vp(property.windowRect.height - avoidArea.bottomRect.height);
      // 监视软键盘的弹出和收起
      currentWindow.on('avoidAreaChange',
      async data => {
        if (data.type !== window.AvoidAreaType.TYPE_KEYBOARD) {
          return;
        }
        this.screenHeight = px2vp(data.area.bottomRect.height);
        console.log(this.screenHeight + '' + 'foo xxx'
        ) //控制台输出可以看到键盘的高度
      }
      )
      //方式2:直接获取软键盘高度
      currentWindow.on('keyboardHeightChange', data => {
        console.log("foo data" + px2vp(data))
        //控制台输出可以看到键盘的高度
      }
      )
    })
}

参考链接为:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进