HarmonyOS 键盘关闭问题

一个flutter页面或是webview页面打开弹起的键盘,手势返回时,如何关闭键盘

HarmonyOS
2025-01-09 16:32:37
729浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

使用示例2的demo。把// PageTwo.ets换成下面的,您试一下。

跳转到PageTwo页面后 点击 keyboardHeightChange 开启监听软键盘高度

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#示例2

// PageTwo.ets
import web_webview from '@ohos.web.webview';
import { inputMethod } from '@kit.IMEKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';

@Builder
export function PageTwoBuilder(name: string, param: Object) {
  PageTwo()
}

@Component
export struct PageTwo {
  @State keyBoardHeight: number = 0;
  pathStack: NavPathStack = new NavPathStack()
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();
  private menuItems: Array<NavigationMenuItem> = [
    {
      value: "1",
      icon: 'resources/base/media/undo.svg',
    },
    {
      value: "2",
      icon: 'resources/base/media/redo.svg',
      isEnabled: false,
    },
    {
      value: "3",
      icon: 'resources/base/media/ic_public_ok.svg',
      isEnabled: true,
    }
  ]

  aboutToAppear(): void {

  }

  build() {
    NavDestination() {
      Column() {
        // web组件加载本地local1.html页面
        Web({ src: $rawfile('local1.html'), controller: this.webviewController })  .height(50)
        Button('keyboardHeightChange', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            window.getLastWindow(getContext()).then(lastWindow => {
              lastWindow.on('keyboardHeightChange', (data) => {
                this.keyBoardHeight = data
                console.info('keyBoardHeight: '+ data.toString())
              })
            }).catch((err: BusinessError) => {
              console.error('jjxs:Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(err));
            })
          })
        Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pathStack.pushPathByName('pageOne', null)
          })
      }.width('100%').height('100%')
    }.title('pageTwo')
    .menus(this.menuItems)
    .onBackPressed(() => {
      if ( this.keyBoardHeight>0) {
        let inputMethodController = inputMethod.getController();
        inputMethodController.hideTextInput()
        return true
      }
      this.pathStack.pop()
      return true
    })
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
      console.log("current page config info is " + JSON.stringify(context.getConfigInRouteMap()))
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
分享
微博
QQ
微信
回复
2025-01-09 18:13:07


相关问题
HarmonyOS 如何先关闭键盘关闭弹窗
1290浏览 • 1回复 待解决
HarmonyOS 如何全局关闭键盘
753浏览 • 1回复 待解决
HarmonyOS键盘如何主动关闭
618浏览 • 1回复 待解决
怎么主动关闭键盘,有人知道吗?
1085浏览 • 1回复 待解决
怎么监听键盘的弹起和关闭事件?
3563浏览 • 1回复 待解决
HarmonyOS emitter关闭问题
1110浏览 • 1回复 待解决
HarmonyOS 页面关闭问题
529浏览 • 1回复 待解决
HarmonyOS 键盘问题
581浏览 • 1回复 待解决
HarmonyOS 页面栈关闭问题
989浏览 • 1回复 待解决
HarmonyOS 键盘InputType支持问题
662浏览 • 1回复 待解决
HarmonyOS 键盘事件监听问题
1539浏览 • 1回复 待解决
HarmonyOS textinput键盘弹出问题
1408浏览 • 1回复 待解决
HarmonyOS web弹起键盘问题
785浏览 • 1回复 待解决
HarmonyOS键盘问题
506浏览 • 1回复 待解决