排查弹窗创建的相关的代码。创建子窗时传递的参数是随时间动态变化的,每次点击操作都会随时间的变化创建新的子窗。moveWindowToAsync方法中传入的参数是固定值,所创建的子窗口的位置因此是固定的。多次点击,会在同一位置创建不同名字的窗口。
// 创建子窗,每次创建时使用不同的子窗名字
this.windowStage?.createSubWindow(`subWindowDialog${Date.now()}`).then((windowClass: window.Window) => {
// 子窗加载页面
// 此处仅为示例,需要更换为实际页面,并通过LocalStorage传入状态属性
windowClass.loadContent('pages/DialogTest', new LocalStorage(), () => {
// 设置子窗口的宽高,高度是屏幕高度的一半再减去导航条区域的高度
windowClass.resize(screen.width / 2, screen.height / 2 - this.getUIContext().vp2px(40));
// 设置子窗口的背景色
windowClass.setWindowBackgroundColor('#33000000');
// 移动子窗口的位置
windowClass.moveWindowToAsync(screen.width / 4, screen.height / 2);
// 显示子窗口
windowClass.showWindow();
});
}); // 创建弹窗的方法,subWindow方案
// 此处仅为示例,实际现象是仅平板横屏时弹窗未遮挡按钮
createWindow() {
// 获取屏幕数据
const screen = display.getDefaultDisplaySync();
// 创建子窗,传入固定的子窗名字
this.windowStage?.createSubWindow(`subWindowDialog`).then((windowClass: window.Window) => {
// 子窗加载页面
// 此处仅为示例,需要更换为实际页面,并通过LocalStorage传入状态属性
windowClass.loadContent('pages/DialogTest', new LocalStorage(), () => {
// 设置子窗口的宽高,高度是屏幕高度的一半再减去导航条区域的高度
windowClass.resize(screen.width / 2, screen.height / 2 - this.getUIContext().vp2px(40));
// 设置子窗口的背景色
windowClass.setWindowBackgroundColor('#ffffffff');
// 移动子窗口的位置
windowClass.moveWindowToAsync(screen.width / 4, screen.height / 2);
// 显示子窗口
windowClass.showWindow();
});
});
}
效果如下:

// CustomDialog弹窗的builder函数
@Builder
customDialogComponent() {
Scroll() {
Column() {
Row({ space: 50 }) {
Text('这是一个弹窗')
}.height(200).padding(5)
}
}
}
// 打开弹窗
openDialog() {
this.getUIContext().getPromptAction().openCustomDialog({
// 弹出框的内容
builder: () => {
// @builder装饰器所装饰的函数
this.customDialogComponent();
},
// 使用模态窗
isModal: true,
// 蒙层颜色
maskColor: '#33000000',
});
} 效果如下:

完整代码如下:
import { display, window } from '@kit.ArkUI';
@Entry
@Component
struct DialogTest {
// 需要提前在EntryAbility的onWindowStageCreate声明周期中使用AppStorage保存windowStage实例
// 在这里取出存储的windowStage实例
@StorageLink('windowStage') windowStage: window.WindowStage | undefined = AppStorage.get('windowStage');
// 创建弹窗的方法,subWindow方案
// 此处仅为示例,实际现象是仅平板横屏时弹窗未遮挡按钮
createWindow() {
// 获取屏幕数据
const screen = display.getDefaultDisplaySync();
// 创建子窗,传入固定的子窗名字
this.windowStage?.createSubWindow(`subWindowDialog`).then((windowClass: window.Window) => {
// 子窗加载页面
// 此处仅为示例,需要更换为实际页面,并通过LocalStorage传入状态属性
windowClass.loadContent('pages/DialogTest', new LocalStorage(), () => {
// 设置子窗口的宽高,高度是屏幕高度的一半再减去导航条区域的高度
windowClass.resize(screen.width / 2, screen.height / 2 - this.getUIContext().vp2px(40));
// 设置子窗口的背景色
windowClass.setWindowBackgroundColor('#ffffffff');
// 移动子窗口的位置
windowClass.moveWindowToAsync(screen.width / 4, screen.height / 2);
// 显示子窗口
windowClass.showWindow();
});
});
}
// CustomDialog弹窗的builder函数
@Builder
customDialogComponent() {
Scroll() {
Column() {
Row({ space: 50 }) {
Text('这是一个弹窗')
}.height(200).padding(5)
}
}
}
// 打开弹窗
openDialog() {
this.getUIContext().getPromptAction().openCustomDialog({
// 弹出框的内容
builder: () => {
// @builder装饰器所装饰的函数
this.customDialogComponent();
},
// 使用模态窗
isModal: true,
// 蒙层颜色
maskColor: '#33000000',
});
}
build() {
Column({ space: 20 }) {
Button('弹出弹框-CustomDialog')
.margin({ top: 40 })
.onClick(() => {
this.openDialog();
});
Button('弹出弹框-SubWindow')
.onClick(() => {
this.createWindow();
})
Button('关闭弹窗-SubWindow')
.onClick(() => {
window.getLastWindow(this.getUIContext().getHostContext()).then((windowClass) => {
windowClass.destroyWindow();
});
})
}
.width('100%')
.height('100%')
}
}