HarmonyOS 工具类自定义弹窗方案咨询

需要实现自定义样式的弹窗,但发现自定义弹窗API只能Component中创建

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

HarmonyOS
2024-12-24 16:58:15
843浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

全局自定义弹窗可参考:https://developer.huawei.com/consumer/cn/forum/topic/0208150480860650009?fid=0109140870620153026,或:https://gitee.com/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/customdialog/README.md,或可以参考如下代码:Index.ets

import { GlobalContext } from '../model/GlobalContext';
import { testPromptDialog } from '../model/HttpUtil';

@Entry
@Component
struct Index {
  aboutToAppear(): void {
    GlobalContext.getContext().setObject('UIContext', this)
  }
  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.

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.

HttpUtil.ets

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

let customDialogId: number = 0
@Builder
export function customDialogBuilder(content: String) {
  Column() {
    Text(${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;
      })
  }
}
  • 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.
  • 27.
  • 28.
  • 29.
  • 30.
分享
微博
QQ
微信
回复
2024-12-24 20:25:07
相关问题
HarmonyOS 自定义相册方案咨询
770浏览 • 1回复 待解决
HarmonyOS 自定义注解的实现方案
940浏览 • 1回复 待解决
HarmonyOS 在ArkTS中想调用自定义弹窗
792浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1958浏览 • 1回复 待解决
HarmonyOS Button stateEffect自定义咨询
514浏览 • 1回复 待解决
HarmonyOS 自定义扫码咨询
634浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
1332浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog
773浏览 • 1回复 待解决
HarmonyOS自定义相册选择页面咨询
1066浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
1363浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
795浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
1630浏览 • 1回复 待解决
HarmonyOS 自定义弹窗层级问题
978浏览 • 1回复 待解决
HarmonyOS 自定义弹窗控制问题
983浏览 • 1回复 待解决