HarmonyOS 如何不在组件内打开自定义弹框

HarmonyOS
2025-01-09 16:14:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,具体用法参考示例:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5#customdialogcontroller

可以使用promptAction.openCustomDialog接口,全局调用自定义弹窗打开,文档参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#promptactionopencustomdialog11

参考示例如下:

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

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

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

4、使用弹窗

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

  build() {
    Button("promptAction弹窗")
      .onClick(() => {
        testPromptDialog()
      })
  }
}
分享
微博
QQ
微信
回复
2025-01-09 19:05:57
相关问题
HarmonyOS 自定义组件问题
907浏览 • 1回复 待解决
HarmonyOS 自定义封装问题
391浏览 • 1回复 待解决
HarmonyOS 自定义不能全屏
496浏览 • 1回复 待解决
HarmonyOS 如何设置自定义的颜色
334浏览 • 1回复 待解决
自定义的状态获取
1223浏览 • 1回复 待解决
如何自定义加上圆角背景
2394浏览 • 1回复 待解决
HarmonyOS 背景色如何自定义图片
302浏览 • 1回复 待解决
是否可以自定义权限文字
2057浏览 • 1回复 待解决
HarmonyOS app版本升级需要自定义
548浏览 • 1回复 待解决
HarmonyOS 自定义遮罩透传问题
322浏览 • 1回复 待解决
自定义,遮罩背景颜色无法设置
642浏览 • 1回复 待解决
HarmonyOS 自定义关闭后页面上移
378浏览 • 1回复 待解决
HarmonyOS 自定义导致机测不通过
437浏览 • 1回复 待解决