HarmonyOS 有没有一种方法可以把全局的自定义dialog都关闭?

有没有一种方法可以把全局的自定义dialog都关闭?现在有一个场景是在app内,A页面触发某个接口需要跳转到登录页面,当A页面有自定义弹窗时,登录页面会被自定义弹窗盖住,有没有一种方法可以全局把弹窗关闭?弹窗太多,不太适合每个弹窗都监听跳转登录页面然后关闭本身的方法处理。

HarmonyOS
2024-10-15 11:24:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

可以创建一个全局弹窗管理的对象,注册全局事件来维护弹窗弹窗对象。参考代码如下:

EntryAbility.ets文件。

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {  
    let context = this.context;  
    let eventhub = this.context.eventHub;  
    AppStorage.setOrCreate('dialogList', []);  
    eventhub.on('addDiaLog', (data: CustomDialogController ) => {  
          let dialogList:SubscribedAbstractProperty<CustomDialogController[]> = AppStorage.link('dialogList');  
          let dialoginfo:CustomDialogController[] = dialogList.get();  
          dialoginfo.push(data);  
          dialogList.set(dialoginfo)  
     });  
    eventhub.on('closeAllDiaLog', ()=>{  
         let dialogList:SubscribedAbstractProperty<CustomDialogController[]> = AppStorage.link('dialogList');  
         let dialoginfo:CustomDialogController[] = dialogList.get();  
         for (let elem of dialoginfo.values()) {  
               elem.close();  
         }  
    })  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');  
}

Index.ets文件。

import common from '@ohos.app.ability.common';  
@CustomDialog  
struct CustomDialogExampleTwo {  
  private context = getContext(this) as common.UIAbilityContext;  
  controllerTwo?: CustomDialogController  
  build() {  
    Column() {  
      Text('我是第二个弹窗')  
        .fontSize(30)  
        .height(100)  
      Button('点我关闭所有弹窗')  
        .onClick(() => {  
          this.context.eventHub.emit('closeAllDiaLog');  
          // if (this.controllerTwo != undefined) {  
          // this.controllerTwo.close()  
          // }  
        })  
        .margin(20)  
    }  
  }  
}  
@CustomDialog  
struct CustomDialogExample {  
  private context = getContext(this) as common.UIAbilityContext;  
  @Link textValue: string  
  @Link inputValue: string  
  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({  
    builder: CustomDialogExampleTwo(),  
    alignment: DialogAlignment.Bottom,  
    offset: { dx: 0, dy: -25 } })  
  controller?: CustomDialogController  
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面  
  cancel: () => void = () => {  
  }  
  confirm: () => void = () => {  
  }  
  aboutToAppear() {  
    this.context.eventHub.emit('addDiaLog', this.dialogControllerTwo);  
  }  
  build() {  
    Column() {  
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })  
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')  
        .onChange((value: string) => {  
          this.textValue = value  
        })  
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })  
      Flex({ justifyContent: FlexAlign.SpaceAround }) {  
        Button('cancel')  
          .onClick(() => {  
            if (this.controller != undefined) {  
              this.controller.close()  
              this.cancel()  
            }  
          }).backgroundColor(0xffffff).fontColor(Color.Black)  
        Button('confirm')  
          .onClick(() => {  
            if (this.controller != undefined) {  
              this.inputValue = this.textValue  
              this.controller.close()  
              this.confirm()  
            }  
          }).backgroundColor(0xffffff).fontColor(Color.Red)  
      }.margin({ bottom: 10 })  
  
      Button('点我打开第二个弹窗')  
        .onClick(() => {  
  
          if (this.dialogControllerTwo != null) {  
            this.dialogControllerTwo.open()  
          }  
        })  
        .margin(20)  
    }.borderRadius(10)  
    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。  
  }  
}  
@Entry  
@Component  
struct Index {  
  private context = getContext(this) as common.UIAbilityContext;  
  @State textValue: string = ''  
  @State inputValue: string = 'click me'  
  dialogController: CustomDialogController | null = new CustomDialogController({  
    builder: CustomDialogExample({  
      cancel: ()=> { this.onCancel() },  
      confirm: ()=> { this.onAccept() },  
      textValue: $textValue,  
      inputValue: $inputValue  
    }),  
    cancel: this.exitApp,  
    autoCancel: true,  
    alignment: DialogAlignment.Bottom,  
    offset: { dx: 0, dy: -20 },  
    gridCount: 4,  
    customStyle: false,  
    cornerRadius: 10,  
  })  
  aboutToAppear() {  
    this.context.eventHub.emit('addDiaLog', this.dialogController);  
  }  
  // 在自定义组件即将析构销毁时将dialogControlle置空  
  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')  
  }  
  exitApp() {  
    console.info('Click the callback in the blank area')  
  }  
  build() {  
    Column() {  
      Button(this.inputValue)  
        .onClick(() => {  
          if (this.dialogController != null) {  
            this.dialogController.open()  
          }  
        }).backgroundColor(0x317aff)  
    }.width('100%').margin({ top: 5 })  
  }  
}

可以使用promptAction.openCustomDialog弹框,打开弹框会有一个dialogId,将每个dialogId保存到一个数组里面,关闭的时候可以循环这个数组,使用promptAction.closeCustomDialog(dialogId) 关闭对应的弹框,建议还是使用遍历dialogid的方式关闭。文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#promptactionopencustomdialog11

分享
微博
QQ
微信
回复
2024-10-15 16:42:49
相关问题
HarmonyOS 自定义全局dialog
195浏览 • 1回复 待解决
HarmonyOS 自定义dialog封装后全局调用
246浏览 • 1回复 待解决
HarmonyOS 如何自己定义一种结构
159浏览 • 1回复 待解决
如何在全局实现自定义dialog弹窗
3124浏览 • 1回复 待解决
HarmonyOS 全局设置自定义字体方法
221浏览 • 1回复 待解决
HarmonyOS 有没有类似Canvas自定义view
503浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
421浏览 • 1回复 待解决
HarmonyOS 自定义全屏dialog
342浏览 • 1回复 待解决