HarmonyOS鸿蒙Next中CustomDialog自定义弹窗组件如何进行物理返回拦截

HarmonyOS鸿蒙Next中CustomDialog自定义弹窗组件如何进行物理返回拦截 执行点击遮障层关闭、侧滑(左滑或右滑)、三键Back、键盘ESC关闭等交互操作时,如果注册了回调函数,弹窗不会立即关闭。

在回调函数中,通过reason获取阻拦关闭弹窗的操作类型,根据原因决定是否关闭弹窗。

示例代码如下:

@CustomDialog
struct CustomDialogExample {
  cancel?: () => void;
  confirm?: () => void;
  controller: CustomDialogController;


  build() {
    Column() {
      Text('Are you sure?')
        .fontSize(20)
        .margin({
          top: 10,
          bottom: 10
        })
      Row() {
        Button('cancel')
          .onClick(() => {
            this.controller.close();
            if (this.cancel) {
              this.cancel();
            }
          })
          .backgroundColor(0xffffff)
          .fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            this.controller.close();
            if (this.confirm) {
              this.confirm();
            }
          })
          .backgroundColor(0xffffff)
          .fontColor(Color.Red)
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceAround)
      .margin({ bottom: 10 })
    }
  }
}


@Component
export struct InterceptCustomDialog {
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => {
        this.onCancel();
      },
      confirm: () => {
        this.onAccept();
      }
    }),
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
      console.log('dialog onWillDismiss reason: ' + dismissDialogAction.reason);
      // 1、PRESS_BACK    Click the back button, swipe left/right, and ESC on the keyboard.
      // 2、TOUCH_OUTSIDE    When clicking on the barrier layer
      // 3、CLOSE_BUTTON    Click the close button
      if (dismissDialogAction.reason === DismissReason.PRESS_BACK) {
        // After processing the business logic, actively close the dialog box through 'dismiss'
        // dismissDialogAction.dismiss();
      }
      if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) {
        // dismissDialogAction.dismiss();
      }
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 }
  })


  onCancel() {
    console.info('Callback when the first button is clicked');
  }


  onAccept() {
    console.info('Callback when the second button is clicked');
  }


  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          this.dialogController.open();
        })
    }
    .width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中CustomDialog自定义弹窗组件如何进行物理返回拦截的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

若不调用  dismissDialogAction.dismiss(),对话框就不会关闭,可用于拦截用户误操作。

更多关于HarmonyOS鸿蒙Next中CustomDialog自定义弹窗组件如何进行物理返回拦截的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,拦截CustomDialog的物理返回键,需在自定义弹窗的onBackPress回调中处理。具体步骤:在自定义弹窗组件中重写onBackPress方法,返回true即可拦截返回事件。示例代码片段如下:

onBackPress(): boolean {
  // 执行自定义逻辑
  return true; // 拦截返回键
}

此方法会阻止物理返回键关闭弹窗,需自行管理弹窗的关闭逻辑。

在HarmonyOS Next中,通过CustomDialogControlleronWillDismiss回调函数可以拦截物理返回等关闭操作。当用户触发遮障层点击、侧滑、三键Back或键盘ESC时,系统会先调用此回调,并传入DismissDialogAction对象。

关键点在于dismissDialogAction.reason,它标识了关闭原因:

  • DismissReason.PRESS_BACK:对应物理返回键、侧滑手势、键盘ESC
  • DismissReason.TOUCH_OUTSIDE:点击遮障层
  • DismissReason.CLOSE_BUTTON:点击关闭按钮(若设置)

onWillDismiss中,你可以根据reason进行业务判断。若需阻止关闭,不调用dismissDialogAction.dismiss()即可;若允许关闭,则主动调用该方法。你的示例代码正确展示了如何区分PRESS_BACKTOUCH_OUTSIDE,并在注释处预留了业务处理逻辑。

这种机制允许你在弹窗关闭前进行数据校验、二次确认等操作,实现了对物理返回等系统级操作的精确控制。

回到顶部