HarmonyOS鸿蒙Next中dialog展示时不占据焦点

HarmonyOS鸿蒙Next中dialog展示时不占据焦点 如题:dialog弹出的时候,其他按钮就不能点击了 有没有什么方式 弹出对话框 还能对其他组件做操作

3 回复
您的意思是弹出弹窗之后弹窗下面的按钮 无法操作么? `isModal: false` 即可

```javascript
// xxx.ets

[@CustomDialog](/user/CustomDialog)
struct CustomDialogExample {
  controller?: CustomDialogController

  cancel: () => void = () => {
  }

  confirm: () => void = () => {
  }

  build() {
    Column() {
      Text('可展示在主窗口外的弹窗')
        .fontSize(30)
        .height(100)
      Button('点我关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }
  }
}

[@Entry](/user/Entry)
[@Component](/user/Component)
struct CustomDialogUser {
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => { this.onCancel() },
      confirm: () => { this.onAccept() }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    gridCount: 4,
    showInSubWindow: true,
    isModal: false,
    customStyle: false,
    cornerRadius: 10,
    backgroundColor: Color.Green
  })
  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

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

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

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
      Button("点击测试").margin({top: 10}).onClick(() => {
        console.log("点击测试")
      })
    }.width('100%').margin({ top: 5 })
  }
}

更多关于HarmonyOS鸿蒙Next中dialog展示时不占据焦点的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,Dialog展示时不占据焦点是通过设置Dialog的Focusable属性为false来实现的。默认情况下,Dialog会获取焦点,导致其他组件无法接收输入事件。通过将Focusable属性设置为false,Dialog在展示时不会抢占焦点,用户仍然可以与背景中的其他组件进行交互。具体实现可以通过以下代码示例:

let dialogController = new DialogController();
dialogController.setFocusable(false);
dialogController.showDialog();

这种方式确保了Dialog的展示不会影响用户对其他组件的操作。

在HarmonyOS鸿蒙Next中,默认情况下Dialog会占据焦点,阻止用户与背景界面交互。若希望Dialog展示时不占据焦点,可以通过以下方法实现:

  1. 使用setFocusable(false)方法,将Dialog设置为不占用焦点。
  2. 使用setCanceledOnTouchOutside(true),允许用户点击Dialog外部关闭Dialog,间接实现不占据焦点的效果。

例如:

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setFocusable(false);
dialog.setCanceledOnTouchOutside(true);
dialog.show();

这样,Dialog显示时不会阻止用户与背景界面交互。

回到顶部