HarmonyOS 自定义弹窗优先级?

1、请问自定义弹窗如何控制层级关系,比如同时有A、B、C三个弹窗,怎样保持三个弹窗固定的层级顺序?

2、我想在当前(任意页面)弹出一个弹窗,让它优先级最高,比如强制升级或者隐私协议签署,用户不操作就不能用这种

阅读 455
1 个回答

1、要改变弹窗的层级顺序和数量,需要修改fun方法的dialogControllerArray参数,第一个参数代表的弹窗在最下层,之后的参数代表的弹窗依次覆盖,最后一个参数代表的弹窗在最上层;

@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController
  message1?: string
  message2?: string
  color?: ResourceColor
  length?: number
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }
  build() {
    Column() {
      Text(this.message1)
        .fontSize(30)
        .height(100)
      Button(this.message2)
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }.backgroundColor(this.color)
    .margin({top: this.length, left: this.length})
  }
}

@Entry
@Component
struct Dialog_240704182402056 {
  @State isAutoCancel: boolean = true;
  dialogController1: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({message1:'这是自定义弹窗1',message2:'关闭自定义弹窗1', color: Color.Gray, length: 0}),
    autoCancel: this.isAutoCancel,
    customStyle: true,
    isModal: false
  })

  dialogController2: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({message1:'这是自定义弹窗2',message2:'关闭自定义弹窗2', color: Color.Blue, length: 150}),
    autoCancel: this.isAutoCancel,
    customStyle: true,
    isModal: false
  })

  dialogController3: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({message1:'这是自定义弹窗3',message2:'关闭自定义弹窗3', color: Color.Orange, length: -150}),
    autoCancel: this.isAutoCancel,
    customStyle: true,
    isModal: false
  })

  @State dialogControllerArray: Array<CustomDialogController> = [ this.dialogController2, this.dialogController1, this.dialogController3];

  build() {
    Column() {
      Button('显示弹窗')
        .onClick(() => {
          this.fun(this.dialogControllerArray);
        })
    }
    .width('100%')
    .height('100%')
  }

  fun(dialogControllerArray:Array<CustomDialogController>) {
    for(let i = 0; i < dialogControllerArray.length; i++) {
      dialogControllerArray[i].open();
    }
  }

2、可以通过将promptAction.showDialog()的showInSubWindow属性设置为true来实现,使弹窗作为独立子窗口显示在应用外,就不会被应用内的弹窗遮挡。

例如:在之前的demo中加入如下代码:

aboutToAppear(): void {
  promptAction.showDialog({
    title: '提醒',
    message: '请升级系统',
    showInSubWindow: true,
    buttons: [{text:'升级', color: '#666666'},{text:'取消', color: '#666666'}]
  })
  setTimeout(() => {
  this.fun(this.dialogControllerArray)
},5000)
}

先显示升级弹窗,即使后来再出现新的弹窗也不会覆盖当前弹窗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进