不依赖UI组件的全局自定义弹出框 (openCustomDialog)存在两种入参方式创建自定义弹出框,ComponentContent和Builder:
export class CustomerPromptAction { static ctx: UIContext; static contentNode: ComponentContent<Object>; static options: promptAction.BaseDialogOptions; // 定义方法,从外部传入上下文。 static setContext(context: UIContext) { CustomerPromptAction.ctx = context; } // 配置内容节点。 static setContentNode(context: UIContext, para: Params[]) { let contentNode: ComponentContent<Object> = new ComponentContent(context, wrapBuilder(buildText), para); CustomerPromptAction.contentNode = contentNode; } // 弹窗配置项。 static setOptions(options: promptAction.BaseDialogOptions) { CustomerPromptAction.options = options; } // 定义弹窗打开的方法。 static openDialog(options: promptAction.BaseDialogOptions, context: UIContext, para: Params[]) { CustomerPromptAction.setContext(context); CustomerPromptAction.setContentNode(context, para); CustomerPromptAction.setOptions({ alignment: DialogAlignment.Bottom, offset: { dx: 0, dy: -20 } }); if (CustomerPromptAction.contentNode !== null) { CustomerPromptAction.ctx.getPromptAction() .openCustomDialog(CustomerPromptAction.contentNode, options) .then(() => { }) .catch(() => { }); } } // 关闭弹窗。 static closeDialog() { if (CustomerPromptAction.contentNode !== null) { CustomerPromptAction.ctx.getPromptAction() .closeCustomDialog(CustomerPromptAction.contentNode) .then(() => { }) .catch(() => { }); } } }
export class Params { title: string = ""; iconName: Resource; click: (index: number) => void; constructor(title: string, iconName: Resource, click: (index: number) => void) { this.title = title; this.iconName = iconName; this.click = click; } }
// 点击弹窗中按钮的回调,并根据返回的index判断逻辑。 callBack = (index: number) => { switch (index) { case 0: this.getUIContext().getPromptAction().showToast({ message: '点击了关闭', duration: 2000 }); break; case 1: this.getUIContext().getPromptAction().showToast({ message: '点击了刷新', duration: 2000 }); break; case 2: this.getUIContext().getPromptAction().showToast({ message: '点击了帮助', duration: 2000 }); break; } };
@State arr: Params[] = [ new Params('关闭', $r('app.media.startIcon'), this.callBack), new Params('刷新', $r('app.media.startIcon'), this.callBack), new Params('帮助', $r('app.media.startIcon'), this.callBack)];
完整示例参考如下:
import { ComponentContent, promptAction } from '@kit.ArkUI'; import { UIContext } from '@ohos.arkui.UIContext'; import { Params } from './Params'; @Builder function buildText(params: Params[]) { Column() { Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) { ForEach(params, (item: Params, index: number) => { Column() { Image(item.iconName) .width(30) .height(30) .margin({ top: 20 }) .onClick(() => { item.click(index); }); Text(item.title) .fontSize(15) .margin({ top: 20 }); }; }); } .width('100%'); Text('取消') .margin({ top: 20 }) .height(50) .width('100%') .textAlign(TextAlign.Center) .fontColor(Color.Blue) .onClick(() => { CustomerPromptAction.closeDialog(); }); } .backgroundColor('#FFF0F0F0') .width('100%') .borderRadius(10); } export class CustomerPromptAction { static ctx: UIContext; static contentNode: ComponentContent<Object>; static options: promptAction.BaseDialogOptions; // 定义方法,从外部传入上下文。 static setContext(context: UIContext) { CustomerPromptAction.ctx = context; } // 配置内容节点。 static setContentNode(context: UIContext, para: Params[]) { let contentNode: ComponentContent<Object> = new ComponentContent(context, wrapBuilder(buildText), para); CustomerPromptAction.contentNode = contentNode; } // 弹窗配置项。 static setOptions(options: promptAction.BaseDialogOptions) { CustomerPromptAction.options = options; } // 定义弹窗打开的方法。 static openDialog(options: promptAction.BaseDialogOptions, context: UIContext, para: Params[]) { CustomerPromptAction.setContext(context); CustomerPromptAction.setContentNode(context, para); CustomerPromptAction.setOptions({ alignment: DialogAlignment.Bottom, offset: { dx: 0, dy: -20 } }); if (CustomerPromptAction.contentNode !== null) { CustomerPromptAction.ctx.getPromptAction() .openCustomDialog(CustomerPromptAction.contentNode, options) .then(() => { }) .catch(() => { }); } } // 关闭弹窗。 static closeDialog() { if (CustomerPromptAction.contentNode !== null) { CustomerPromptAction.ctx.getPromptAction() .closeCustomDialog(CustomerPromptAction.contentNode) .then(() => { }) .catch(() => { }); } } }
import { Params } from './Params'; import { CustomerPromptAction } from './CustomerPromptAction'; @Entry @Component struct Index { // 点击弹窗中按钮的回调,并根据返回的index判断逻辑。 callBack = (index: number) => { switch (index) { case 0: this.getUIContext().getPromptAction().showToast({ message: '点击了关闭', duration: 2000 }); break; case 1: this.getUIContext().getPromptAction().showToast({ message: '点击了刷新', duration: 2000 }); break; case 2: this.getUIContext().getPromptAction().showToast({ message: '点击了帮助', duration: 2000 }); break; } }; // 初始化弹窗菜单的数据源。 @State arr: Params[] = [ new Params('关闭', $r('app.media.startIcon'), this.callBack), new Params('刷新', $r('app.media.startIcon'), this.callBack), new Params('帮助', $r('app.media.startIcon'), this.callBack)]; build() { Row() { Column() { Button('横向菜单') .margin({ top: 50 }) .onClick(() => { CustomerPromptAction.openDialog({ alignment: DialogAlignment.Bottom, offset: { dx: 0, dy: -20 } }, this.getUIContext(), this.arr); }); } .width('100%') .height('100%'); } .height('100%'); } }
export class Params { title: string = ""; iconName: Resource; click: (index: number) => void; constructor(title: string, iconName: Resource, click: (index: number) => void) { this.title = title; this.iconName = iconName; this.click = click; } }