HarmonyOS 应用级全局弹框

实现应用级全局弹框。

1、不管当前是在哪个页面,都能弹出来,基本排除Navigation。

2、弹框点击返回按钮不会消失,排除系统dialog。

3、不需要任何权限,如系统弹框。

HarmonyOS
2024-10-22 11:42:30
1205浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

promptAction.openCustomDialog 全局弹窗参考代码如下:

一、新建3个工具类

1、GlobalContext.ets

export class GlobalContext {  
  private constructor() {  
  }  
  
  private static instance: GlobalContext;  
  private _objects = new Map<string, Object>();  
  
  public static getContext(): GlobalContext {  
    if (!GlobalContext.instance) {  
      GlobalContext.instance = new GlobalContext();  
    }  
    return GlobalContext.instance;  
  }  
  
  getObject(value: string): Object | undefined {  
    return this._objects.get(value);  
  }  
  
  setObject(key: string, objectClass: Object): void {  
    this._objects.set(key, objectClass);  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

2、DialogUtils.ets

import { promptAction } from '@kit.ArkUI'  
import { GlobalContext } from './GlobalContext'  
  
@Builder  
export function customDialogBuilder(content: String) {  
  Column() {  
    Text(`Tip: ${content} `).fontSize(20).height("30%")  
    Text('失败原因:失败原因失败原因失败原因失败原因失败原因失败原因失败原因!').fontSize(16).height("30%")  
    Row() {  
      Button("确认").onClick(() => {  
        promptAction.closeCustomDialog(getCustomDialogId())  
      })  
      Blank().width(50)  
      Button("取消").onClick(() => {  
        promptAction.closeCustomDialog(getCustomDialogId())  
      })  
    }  
    .margin({ top: 30 })  
  }.height(200).padding(5)  
}  
  
function getCustomDialogId() {  
  // 取customDialogId  
  let customDialogId = GlobalContext.getContext().getObject('customDialogId') as number  
  return customDialogId;  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

3、HttpUtil.ets

import { GlobalContext } from './GlobalContext';  
import { promptAction } from '@kit.ArkUI';  
import { customDialogBuilder } from './DialogUtils';  
  
  
export function testPromptDialog() {  
  // 方法调用后使用弹窗  
  // 使用GlobalContext中UIContext  
  const that = GlobalContext.getContext().getObject('UIContext') as UIContext;  
  if (that) {  
    promptAction.openCustomDialog({  
      builder: customDialogBuilder.bind(that, "网络请求失败!")  
    }).then((dialogId: number) => {  
      GlobalContext.getContext().setObject('customDialogId', dialogId)  
    })  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

二、页面测试弹窗效果

1、Index.ets

import { GlobalContext } from '../utils/GlobalContext'  
import { router } from '@kit.ArkUI'  
  
@Entry  
@Component  
struct Index {  
  aboutToAppear(): void {  
    // 存入UIContext与customDialogId  
    GlobalContext.getContext().setObject('UIContext', this)  
    GlobalContext.getContext().setObject('customDialogId', 0)  
  }  
  
  build() {  
    Row() {  
      Column() {  
        Button("跳转其他页面")  
          .onClick(() => {  
            router.pushUrl({ url: "pages/Page2" })  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

2、Page2.ets

import { testPromptDialog } from '../utils/HttpUtil';  
  
@Entry  
@Component  
struct Page2 {  
  @State message: string = 'Hello World';  
  
  build() {  
    Row() {  
      Column() {  
        Button("promptAction弹窗")  
          .onClick(() => {  
            testPromptDialog()  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
分享
微博
QQ
微信
回复
2024-10-22 17:32:46


相关问题
HarmonyOS 如何实现全局
926浏览 • 1回复 待解决
HarmonyOS 支付输入
1024浏览 • 1回复 待解决
HarmonyOS PermissionDialog无法
694浏览 • 1回复 待解决
HarmonyOS CustomDialog报错
789浏览 • 1回复 待解决
HarmonyOS 隐私协议
712浏览 • 1回复 待解决
HarmonyOS 嵌套的实现
818浏览 • 1回复 待解决
HarmonyOS API调用与Dialog
1026浏览 • 0回复 待解决
HarmonyOS 通知授权无法弹出 -
1069浏览 • 1回复 待解决
HarmonyOS 如何主动隐藏输入
823浏览 • 1回复 待解决
代码获取后台权限?
4303浏览 • 1回复 待解决
获取定位权限没有
2852浏览 • 1回复 待解决
HarmonyOS 软键盘挤压Toast
559浏览 • 1回复 待解决
HarmonyOS 自定义不能全屏
930浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
1285浏览 • 1回复 待解决
HarmonyOS 自定义封装问题
838浏览 • 1回复 待解决
HarmonyOS 底部上滑的那种
826浏览 • 1回复 待解决
HarmonyOS 如何做到页面在之上?
1287浏览 • 1回复 待解决