HarmonyOS web组件中的网页输入框无法被键盘顶起?

web嵌入的网页,输入框无法被键盘顶起,真机自带的浏览器中,网页输入框可以顶起,请问这种情况可以怎么处理呢?

阅读 530
1 个回答

可以通过获取键盘避让区域的高度,添加到web组件底部距离上,手动抬起web组件,demo如下:

import webview from '@ohos.web.webview'
import { KeyboardAvoidMode } from '@kit.ArkUI';
import window from '@ohos.window';
@Entry
@Component
struct B {
  @State scrollHeight: number = 40;
  controller: webview.WebviewController = new webview.WebviewController();
  aboutToAppear() {
    window.getLastWindow(getContext(this)).then(currentWindow => {
      currentWindow.on('avoidAreaChange', async data => {
        //判断规避区是否是软键盘区域。
        if (data.type !== window.AvoidAreaType.TYPE_KEYBOARD) {
          return;
        }
        //规避区的高度。
        this.scrollHeight = px2vp(data.area.bottomRect.height)+40;
      })
    })
  };


  build() {
    Column() {
      Text('客服').height(55)
      Web({
        src: 'https:www.index.com',
        controller: this.controller
      })
        .domStorageAccess(true)
        .onlineImageAccess(true)
        .imageAccess(true)
        .zoomAccess(false)
        .javaScriptAccess(true)
          //手动改变bottom值
        .margin({ bottom: this.scrollHeight })
        .animation({curve: Curve.EaseOut, duration: 300})
    }.width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进