【HarmonyOS 5】使用openCustomDialog如何禁止手势关闭的方案

【HarmonyOS 5】使用openCustomDialog如何禁止手势关闭的方案

一、前言

在HarmonyOS中使用openCustomDialog自定义弹框时,我们会遇到实现禁止手势关闭弹框的业务场景。

虽然在 HarmonyOS Next 中,自定义 Dialog 默认可能继承系统的侧滑返回手势,并且目前无法屏蔽,官方反馈未来版本可能会开放禁用选项。

在当前版本中,目前无法完全禁止手势关闭,但可以通过一些方法进行控制。例如,监听 onWillDismiss 事件可以在关闭时进行拦截,但需要处理不同的关闭原因。

华为官方文档明确提到了使用openCustomDialog时可以通过配置onWillDismiss回调来拦截关闭事件。在onWillDismiss中,可以检查DismissReason来判断关闭原因,例如用户滑动或点击外部。如果是手势关闭(如侧滑),可以通过返回false来阻止对话框关闭。可以通过监听onWillDismiss事件来禁止手势关闭。

openCustomDialog提供了onWillDismiss回调函数,当用户尝试通过滑动、点击外部、返回键等操作关闭弹窗时,会触发该回调。通过在回调中判断关闭原因并拦截操作,即可实现禁止手势关闭的效果。

二、方案思路

1. 定义自定义弹窗组件
typescript 复制代码
import { PromptAction, DismissReason } from '@ohos.prompt';

@Builder
function CustomDialogContent() {
  return Column() {
    Text('禁止手势关闭的弹窗')
      .fontSize(24)
      .fontWeight(FontWeight.Bold)
    Button('确认关闭')
      .onClick(() => {
        // 主动关闭弹窗
        promptAction.closeCustomDialog(dialogId);
      })
  }
  .padding(30)
  .backgroundColor(Color.White)
  .borderRadius(16)
  .width('80%')
}
2. 打开弹窗并设置拦截逻辑
typescript 复制代码
let promptAction = UIContext.getPromptAction();
let dialogId: number = 0;

promptAction.openCustomDialog({
  builder: () => CustomDialogContent(),
  alignment: DialogAlignment.Center,
  maskColor: 'rgba(0, 0, 0, 0.3)',
  autoCancel: false, // 禁止点击外部关闭
  onWillDismiss: (dismissAction) => {
    // 处理不同关闭原因
    switch (dismissAction.reason) {
      case DismissReason.SWIPE: // 侧滑关闭
      case DismissReason.BACK:   // 返回键关闭
        return false; // 阻止关闭
      default:
        return true; // 允许其他方式关闭
    }
  }
}).then(id => dialogId = id);
3. 关闭类型参数说明
参数 说明
autoCancel 控制是否允许点击外部关闭弹窗,设置为false可禁用该功能。
onWillDismiss 关闭事件回调函数,返回false可阻止关闭,返回true则允许关闭。
DismissReason 关闭原因枚举,包含SWIPE(侧滑)、BACK(返回键)等类型。

三、源码DEMO示例

typescript 复制代码
import { PromptAction, DismissReason } from '@ohos.prompt';

@Entry
@Component
struct App {
  private promptAction: PromptAction = UIContext.getPromptAction();
  private dialogId: number = 0;

  build() {
    Column() {
      Button('打开禁止手势关闭的弹窗')
        .onClick(() => this.showDialog())
    }
  }

  showDialog() {
    this.promptAction.openCustomDialog({
      builder: () => this.CustomDialogContent(),
      alignment: DialogAlignment.Center,
      maskColor: 'rgba(0, 0, 0, 0.3)',
      autoCancel: false,
      onWillDismiss: (dismissAction) => {
        console.log(`关闭原因:${dismissAction.reason}`);
        return dismissAction.reason === DismissReason.BUTTON; // 仅允许按钮关闭
      }
    }).then(id => this.dialogId = id);
  }

  @Builder
  CustomDialogContent() {
    return Column() {
      Text('禁止手势关闭')
        .fontSize(24)
        .fontWeight(FontWeight.Bold)
      Button('确认关闭')
        .onClick(() => this.promptAction.closeCustomDialog(this.dialogId))
    }
    .padding(30)
    .backgroundColor(Color.White)
    .borderRadius(16)
    .width('80%')
  }
}

注意

综上所述,可在HarmonyOS中实现openCustomDialog的手势关闭拦截。对于需要完全禁止系统级手势的场景,建议结合页面级导航拦截逻辑进行处理。
系统限制

  • 在HarmonyOS Next系统中,部分系统级手势(如从屏幕边缘向内滑动返回)可能无法完全拦截。
  • 建议通过onWillDismiss回调配合页面级onBackPress拦截实现更全面的控制。
相关推荐
黑臂麒麟17 小时前
HarmonyOS鸿蒙实战应用7:随手账本——数据导出与分享
华为·app·arkts·鸿蒙
贾伟康19 小时前
【天体运行模拟|11】HarmonyOS ArkTS 收藏与笔记实战:同步知识页和个人学习记录
harmonyos·arkts·preferences·多设备适配·arkdata
2501_9197490319 小时前
华为鸿蒙录音可视化APP—小羊声觉
华为·harmonyos·鸿蒙
大雷神20 小时前
HarmonyOS AR Engine深度估计实战——把毫米距离画成实时热力图
华为·ar·harmonyos
GKxx20 小时前
在 HarmonyOS 上给 QEMU 搭一个最小 aarch64 Linux guest(内核 + busybox initramfs)
linux·华为·qemu·harmonyos·鸿蒙·鸿蒙pc
黑臂麒麟20 小时前
屏幕信息全解析:HarmonyOS的分辨率、刷新率、折叠状态一个API搞定
华为·arkts·鸿蒙
黑臂麒麟20 小时前
HarmonyOS 网络连接诊断实战:检测网络状态、WiFi 切换、弱网监测一网打尽
网络·华为·arkts·鸿蒙
贾伟康21 小时前
【天体运行模拟|08】HarmonyOS ArkTS 单位换算实战:处理天文尺度、科学计数与精度
harmonyos·arkts·arkui·数值精度·单位换算
lilian23321 小时前
HarmonyOS 7 新特性(十三)|3DGS 模型加载、交互与性能回退
3d·交互·harmonyos
2501_919749031 天前
华为鸿蒙免费反诈APP—小羊反诈
华为·harmonyos·鸿蒙