HarmonyOS promptAction.openCustomDialog如何实现点击弹窗阴影区域时弹窗不关闭

HarmonyOS
2025-01-09 17:31:15
497浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可以使用 onWillDismiss方法。当用户执行点击遮障层关闭、左滑/右滑、三键back、键盘ESC关闭交互操作时,如果注册该回调函数,则不会立刻关闭弹窗。在回调函数中可以通过reason得到阻拦关闭弹窗的操作类型,从而根据原因选择是否能关闭弹窗。

参考示例如下:

import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.arkui.node";
import { PromptAction } from '@kit.ArkUI';

//弹框属性
export class Params {
  text: string = ""
  backgroundColor: string = ""
  opacity: number = 1
  width: number = 0
  height: number = 0
  btnControl: string = ''
}

//自定义弹框
@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text || '默认内容')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
  }
  .width(params.width || 200)
  .height(params.height || 400)
  .backgroundColor(params.backgroundColor || '#FFF0F0F0')
  .opacity(params.opacity)
}

//弹框控制
export function setDialog(uiContext: UIContext, promptAction: PromptAction, params: Params) {
  let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), params);
  try {
    promptAction.openCustomDialog(contentNode, {
      onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
        if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
          console.log('点击遮罩层事件:',)
          if (params.btnControl !== '关闭控制') {
            dismissDialogAction.dismiss()
          }
        }
      }
    });
  } catch (error) {
    let message = (error as BusinessError).message;
    let code = (error as BusinessError).code;
    console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
  }
}

@Entry
@Component
struct Index {
  @State message: string = "hello"
  private uiContext = this.getUIContext()
  private promptAction = this.getUIContext().getPromptAction()

  build() {
    Row() {
      Column() {
        Button("控制弹框关闭")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.btnControl = '关闭控制'
            setDialog(this.uiContext, this.promptAction, params)
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:11:10


相关问题
promptAction.openCustomDialog 全局弹窗
1217浏览 • 1回复 待解决
promptAction.openCustomDialog 自定义弹窗
768浏览 • 1回复 待解决
HarmonyOS promptAction.openCustomDialog问题
488浏览 • 1回复 待解决