HarmonyOS 如何先关闭键盘 再关闭弹窗

在自定义弹窗上存在一个文本输入框,当点击文本输入框弹出软键盘时,右滑返回目前会同时关闭自定义弹窗和软键盘,如何通过onWillDismiss来控制软键盘显示时先关闭软键盘,不显示时关闭自定义弹窗。

HarmonyOS
2024-09-26 11:55:44
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

该问题可以参考代码实现。参考代码:

import { window } from '@kit.ArkUI'  
@CustomDialog  
struct CustomDialogExample {  
  controller?: CustomDialogController  
  build() {  
    Column() {  
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })  
        // 标记焦点  
        .id('Text').focusable(true)  
      TextInput().height(60).width('90%')  
    }.borderRadius(10)  
  }  
}  
@Entry  
@Component  
struct CustomDialogUser {  
  @State textValue: string = ''  
  @State inputValue: string = 'click me'  
  @State keyboardIsShow: boolean = false // 软键盘是否弹出  
  //窗口的回调,判断键盘的高度(只能判断键盘的起或者终的高度,开发者可以看日志打印,)  
  onPageShow(): void {  
    window.getLastWindow(getContext(this), (err, win) => {  
      win.on("keyboardHeightChange", (data) => {  
        console.log("soft keyboard height: " + data, " zbt")  
        if (data != 0) {  
          this.keyboardIsShow = true  
        } else {  
          this.keyboardIsShow = false  
        }  
      })  
    })  
  }  
  
  dialogController: CustomDialogController | null = new CustomDialogController({  
    builder: CustomDialogExample({  
    }),  
    autoCancel: true,  
    alignment: DialogAlignment.Bottom,  
    offset: { dx: 0, dy: -20 },  
    customStyle: false,  
    gridCount: 4,  
    cornerRadius: 10,  
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {  
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))  
      console.log("dialog onWillDismiss")  
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {  
        if (this.keyboardIsShow) {  
          focusControl.requestFocus('Text')  
        }else {  
          dismissDialogAction.dismiss()  
        }  
      }  
    },  
  })  
  aboutToDisappear() {  
    this.dialogController = null  
  }  
  build() {  
    Column() {  
      Button(this.inputValue)  
        .onClick(() => {  
          if (this.dialogController != null) {  
            this.dialogController.open()  
          }  
        }).backgroundColor(0x317aff)  
    }.width('100%').height('100%').margin({ top: 5 })  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-09-26 17:39:48
相关问题
HarmonyOS 如何全局关闭键盘
752浏览 • 1回复 待解决
HarmonyOS 如何禁止弹窗关闭
1350浏览 • 1回复 待解决
HarmonyOS 键盘关闭问题
489浏览 • 1回复 待解决
全局关闭弹窗如何实现?
1326浏览 • 2回复 待解决
HarmonyOS键盘如何主动关闭
615浏览 • 1回复 待解决
HarmonyOS 自定义弹窗关闭问题
967浏览 • 1回复 待解决
怎么主动关闭键盘,有人知道吗?
1085浏览 • 1回复 待解决
怎么监听键盘的弹起和关闭事件?
3563浏览 • 1回复 待解决