HarmonyOS 全局自定义弹窗的实现

有一个定时弹窗提醒休息的功能,如何达到规定时间,在任意组件页面都可以弹出该提醒弹窗?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

全局弹窗参考示例如下:

// Index.ets
import { GlobalContext } from './GlobalContext';
import { testPromptDialog } from './HttpUtil';

@Entry
@Component
struct Index {
  aboutToAppear(): void {
    GlobalContext.getContext().setObject('UIContext', this)
  }

  build() {
    Row() {
      Column() {
        Button("promptAction弹窗").onClick(() => {
          testPromptDialog()
        })
      }.width('100%')
    }.height('100%')
  }
}

// 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);
  }
}

// HttpUtil.ets
import { GlobalContext } from './GlobalContext';
import { promptAction } from '@kit.ArkUI';

let customDialogId: number = 0

@Builder
export function customDialogBuilder(content: String) {
  Column() {
    Text(`Tip: ${content} `).fontSize(20).height("30%")
    Text('失败原因:失败原因!').fontSize(16).height("30%")
    Row() {
      Button("确认").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button("取消").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }.margin({ top: 30 })
  }.height(200).padding(5)
}

export function testPromptDialog() {
  const that = GlobalContext.getContext().getObject('UIContext') as UIContext;
  if (that) {
    promptAction.openCustomDialog({ builder: customDialogBuilder.bind(that, "网络请求失败!") })
      .then((dialogId: number) => {
        customDialogId = dialogId;
      })
  }
}

// Index.ets
import { GlobalContext } from './GlobalContext';
import { testPromptDialog } from './HttpUtil';

@Entry
@Component
struct Index {
  aboutToAppear(): void {
    GlobalContext.getContext().setObject('UIContext', this)
  }

  build() {
    Row() {
      Column() {
        Button("promptAction弹窗").onClick(() => {
          testPromptDialog()
        })
      }.width('100%')
    }.height('100%')
  }
}

// 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);
  }
}

// HttpUtil.ets
import { GlobalContext } from './GlobalContext';
import { promptAction } from '@kit.ArkUI';

let customDialogId: number = 0

@Builder
export function customDialogBuilder(content: String) {
  Column() {
    Text(`Tip: ${content} `).fontSize(20).height("30%")
    Text('失败原因:失败原因!').fontSize(16).height("30%")
    Row() {
      Button("确认").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button("取消").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }.margin({ top: 30 })
  }.height(200).padding(5)
}

export function testPromptDialog() {
  const that = GlobalContext.getContext().getObject('UIContext') as UIContext;
  if (that) {
    promptAction.openCustomDialog({ builder: customDialogBuilder.bind(that, "网络请求失败!") })
      .then((dialogId: number) => {
        customDialogId = dialogId;
      })
  }
}

// Index.ets
import { GlobalContext } from './GlobalContext';
import { testPromptDialog } from './HttpUtil';

@Entry
@Component
struct Index {
  aboutToAppear(): void {
    GlobalContext.getContext().setObject('UIContext', this)
  }

  build() {
    Row() {
      Column() {
        Button("promptAction弹窗").onClick(() => {
          testPromptDialog()
        })
      }.width('100%')
    }.height('100%')
  }
}

// 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);
  }
}

// HttpUtil.ets
import { GlobalContext } from './GlobalContext';
import { promptAction } from '@kit.ArkUI';

let customDialogId: number = 0

@Builder
export function customDialogBuilder(content: String) {
  Column() {
    Text(`Tip: ${content} `).fontSize(20).height("30%")
    Text('失败原因:失败原因!').fontSize(16).height("30%")
    Row() {
      Button("确认").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button("取消").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }.margin({ top: 30 })
  }.height(200).padding(5)
}

export function testPromptDialog() {
  const that = GlobalContext.getContext().getObject('UIContext') as UIContext;
  if (that) {
    promptAction.openCustomDialog({ builder: customDialogBuilder.bind(that, "网络请求失败!") })
      .then((dialogId: number) => {
        customDialogId = dialogId;
      })
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 全局自定义弹窗demo
311浏览 • 1回复 待解决
HarmonyOS 使用全局自定义弹窗
69浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗无法弹出
27浏览 • 1回复 待解决
如何封装全局自定义弹窗
334浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
2844浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
596浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1170浏览 • 1回复 待解决
HarmonyOS 自定义弹窗问题
723浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
397浏览 • 1回复 待解决
HarmonyOS 全局设置自定义字体方法
14浏览 • 1回复 待解决
HarmonyOS 自定义弹窗控制问题
12浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
635浏览 • 1回复 待解决