实现一个全局可调用弹窗,可通过如下三种方式实现:
import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(): void {
try {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
} catch (err) {
hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err));
}
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
AppStorage.setOrCreate('windowStage', windowStage);
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}; import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
// 获取windowStage
let windowStage = AppStorage.get('windowStage') as window.WindowStage;
let windowClass: window.Window | undefined = undefined;
export class mySubWindow {
createMySubWindowFun(subWindowName: string) {
// 创建子窗口
windowStage.createSubWindow(subWindowName, (err: BusinessError, data) => {
windowClass = data;
data.setUIContent('pages/SubPage', () => {
// 设置子窗口左上角坐标
data.moveWindowTo(150, 300);
// 设置子窗口大小
data.resize(1000, 1000);
// 展示子窗口
data.showWindow();
});
});
}
deleteMySubWindowFun() {
if (windowClass) {
windowClass.destroyWindow();
}
}
} // 子窗口显示页面
import { mySubWindow } from '../utils/SubWindow';
@Entry
@Component
struct SubPage {
mySubWindow: mySubWindow = new mySubWindow();
build() {
Column() {
Text('子窗口页面')
.fontSize(40)
.margin({ top: 30 });
Button('点击关闭')
.onClick(() => {
this.mySubWindow.deleteMySubWindowFun();
})
.margin({ top: 100 });
}
.width('100%')
.height('100%');
}
} import { mySubWindow } from '../utils/SubWindow';
@Entry
@Component
struct Index {
mySubWindow: mySubWindow = new mySubWindow();
build() {
RelativeContainer() {
Text('点击打开弹窗')
.id('HelloWorld')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.mySubWindow.createMySubWindowFun('mySubWindow');
});
}
.height('100%')
.width('100%')
.backgroundColor('#F1F3F5');
}
} 运行效果图如下:

import { CustomDialogView } from './CustomDialogView';
@Component
export struct Dialog {
// 监听外部传入的visible变量,visible值发生变化时触发onChange回调函数
@Watch('onChange') @Link visible: boolean;
onCancel?: () => void;
onConfirm?: () => void;
// 通过CustomDialogController的builder参数绑定弹窗组件CustomDialogView
private controller = new CustomDialogController({
builder: CustomDialogView({
visible: $visible,
onCancel: this.onCancel,
onConfirm: this.onConfirm,
}),
autoCancel: false,
alignment: DialogAlignment.Center,
});
// 当visible的值变化时触发回调
onChange(): void {
if (this.visible) {
this.controller.open();
} else {
this.controller.close();
}
}
// 二次封装的Dialog组件主要通过控制器控制弹窗,不需要任何界面
build() {
}
} @CustomDialog
export struct CustomDialogView {
@Link visible: boolean;
controller: CustomDialogController;
// 弹窗交互事件参数,点击确认和取消按钮时的回调函数
onCancel?: () => void;
onConfirm?: () => void;
build() {
Column() {
Text('自定义弹窗')
.margin({ top: 20, bottom: 40 });
Button('点击关闭弹窗').onClick(() => {
// 重置弹窗显隐参数
this.visible = false;
// 关闭弹窗
this.controller.close();
});
}
.width('100%')
.height(200)
.backgroundColor(Color.White);
}
} import { Dialog } from './Dialog';
@Entry
@Component
export struct CustomDialog {
// 外部定义visible变量作为弹窗组件入参,控制弹窗显隐
@State visible: boolean = false;
build() {
Column({ space: 20 }) {
Button('点击打开弹窗')
// 点击修改visible变量后,visible的值可以被Dialog组件监听并响应
.onClick(() => this.visible = !this.visible)
.margin({ top: 40 });
// 通过双向绑定visible变量,实现外部控制弹窗
Dialog({
visible: $visible,
});
}
.width('100%');
}
} 运行效果图如下:
