使用子窗口实现弹窗在应用开发中具有以下显著优势,可提升交互灵活性和用户体验:
弹窗层级直接关联所属窗口,通过子窗口可避免主窗口内容遮挡弹窗。场景示例:支付弹窗需强制展示时,将焦点切换到子窗口打开弹窗,确保不被其他窗口覆盖。
以下为实现步骤:
windowStage.loadContent('pages/Index', (err) => { AppStorage.setOrCreate('windowStage',windowStage); if (err.code) { return; } hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); });
// 步骤1:将创建窗口封装成函数实现快速调用创建窗口。 export function createSubWindow(sub: SubWindowClass, positionX: number, positionY: number, width: number) { // 1.获取主窗口windowStage。2.创建子窗口。3.设置子窗口属性。4.加载子窗口内容。5.显示子窗口内容 // 获取主窗口windowStage windowStage_ = AppStorage.get('windowStage'); if (windowStage_ == null) { } // 创建子窗口 windowStage_?.createSubWindow(sub.windowName, (err: BusinessError, data) => { let errCode = err.code; if (errCode) { return; } if (data) { sub.subWindowClass = data; // 设置子窗口属性:大小,位置等 // 设置子窗口位置 sub.subWindowClass.moveWindowTo(positionX, positionY, (err: BusinessError) => { if (err.code) { return; } }); // 设置子窗口大小 sub.subWindowClass.resize(width, sub.windowHeight, (err: BusinessError) => { if (err.code) { return; } }); // 设置子窗口是否可触 sub.subWindowClass.setWindowTouchable(true, (err: BusinessError) => { if (err.code) { return; } }); // 加载子窗口页面 sub.subWindowClass.setUIContent(sub.windowPath, (err: BusinessError) => { if (err.code) { return; } }); // 显示子窗口内容 sub.subWindowClass.showWindow((err: BusinessError) => { if (err.code) { return; } }); } }); }
// 步骤2:将关闭子窗口封装成函数实现任意位置调用关闭任意窗口。 export function destroySubWindow(sub: SubWindowClass) { sub.subWindowClass?.destroyWindow((err: BusinessError) => { if (err.code) { return; } }); }
// 步骤3:将窗口尺寸、位置通过参数传入创建函数,实现窗口属性可自定义。 class SubWindowClass { subWindowClass: window.Window | null = null; windowName: string = ''; windowWidth: number = 0; windowHeight: number = 0; positionX: number = 0; positionY: number = 0; windowPath: string = ''; constructor(subWindowClass: window.Window | null, windowName: string, windowWidth: number, windowHeight: number, positionX: number, positionY: number, windowPath: string) { this.subWindowClass = subWindowClass; this.windowName = windowName; this.windowWidth = windowWidth; this.windowHeight = windowHeight; this.positionX = positionX; this.positionY = positionY; this.windowPath = windowPath; } }
build() { Column() { Text(this.message) .margin({ top: 10 }) .fontColor(Color.White); Button() { Text('close sub window1') .padding(8) .borderRadius(8) .backgroundColor(Color.Gray) .fontColor(Color.White); } .margin({ top: 10 }) .onClick(() => { destroySubWindow(sub1); }); } .width('100%') .height('100%') // 步骤4:被调用页面实现,可自定义圆角。 .borderRadius(10) .backgroundColor('#ff4b7fe9'); }
完整示例参考如下:
// 调用页 index.ets import { display, window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { JSON } from '@kit.ArkTS'; let windowStage_: window.WindowStage | null | undefined = null; // 步骤3:将窗口尺寸、位置通过参数传入创建函数,实现窗口属性可自定义。 class SubWindowClass { subWindowClass: window.Window | null = null; windowName: string = ''; windowWidth: number = 0; windowHeight: number = 0; positionX: number = 0; positionY: number = 0; windowPath: string = ''; constructor(subWindowClass: window.Window | null, windowName: string, windowWidth: number, windowHeight: number, positionX: number, positionY: number, windowPath: string) { this.subWindowClass = subWindowClass; this.windowName = windowName; this.windowWidth = windowWidth; this.windowHeight = windowHeight; this.positionX = positionX; this.positionY = positionY; this.windowPath = windowPath; } } let subWindowClass1: window.Window | null = null; let subWindowClass2: window.Window | null = null; export const sub1 = new SubWindowClass(subWindowClass1, 'mysub1', 500, 400, 10, 1520, 'pages/SubWindow'); export const sub2 = new SubWindowClass(subWindowClass2, 'mysub2', 500, 400, 690, 1520, 'pages/SubWindow2'); // 步骤1:将创建窗口封装成函数实现快速调用创建窗口。 export function createSubWindow(sub: SubWindowClass, positionX: number, positionY: number, width: number) { // 1.获取主窗口windowStage。2.创建子窗口。3.设置子窗口属性。4.加载子窗口内容。5.显示子窗口内容 // 获取主窗口windowStage windowStage_ = AppStorage.get('windowStage'); if (windowStage_ == null) { } // 创建子窗口 windowStage_?.createSubWindow(sub.windowName, (err: BusinessError, data) => { let errCode = err.code; if (errCode) { return; } if (data) { sub.subWindowClass = data; // 设置子窗口属性:大小,位置等 // 设置子窗口位置 sub.subWindowClass.moveWindowTo(positionX, positionY, (err: BusinessError) => { if (err.code) { return; } }); // 设置子窗口大小 sub.subWindowClass.resize(width, sub.windowHeight, (err: BusinessError) => { if (err.code) { return; } }); // 设置子窗口是否可触 sub.subWindowClass.setWindowTouchable(true, (err: BusinessError) => { if (err.code) { return; } }); // 加载子窗口页面 sub.subWindowClass.setUIContent(sub.windowPath, (err: BusinessError) => { if (err.code) { return; } }); // 显示子窗口内容 sub.subWindowClass.showWindow((err: BusinessError) => { if (err.code) { return; } }); } }); } // 步骤2:将关闭子窗口封装成函数实现任意位置调用关闭任意窗口。 export function destroySubWindow(sub: SubWindowClass) { sub.subWindowClass?.destroyWindow((err: BusinessError) => { if (err.code) { return; } }); } @Entry @Component struct Index { @State positionY: number = 0; @State positionX: number = 0; @State screenWidth: number = 0; aboutToAppear(): void { this.screenWidth = display.getDefaultDisplaySync().width; } build() { Column() { Row() { Text('我是主页面') .fontColor(Color.White) .margin({ top: 20 }); } .width('100%') .height(150) .backgroundColor('#ffb7afaf') .justifyContent(FlexAlign.Center) .alignItems(VerticalAlign.Top); Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) { Button('open sub window1') .width((this.screenWidth - 30) / 2) .margin({ left: 10 }) .onClick(() => { createSubWindow(sub1, this.positionX, this.positionY, (this.screenWidth - this.getUIContext().vp2px(30)) / 2); }); Button('open sub window2') .width((this.screenWidth - 30) / 2) .margin({ left: 10, right: 10 }) .onClick(() => { createSubWindow(sub2, (this.screenWidth / 2 + this.getUIContext().vp2px(5)), this.positionY, (this.screenWidth - this.getUIContext().vp2px(30)) / 2); }); } .margin({ top: 20 }); Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) { Button('close sub window1') .width((this.screenWidth - 30) / 2) .margin({ left: 10 }) .id('itemOne') .onAppear(() => { setTimeout(() => { const infoOne = this.getUIContext().getComponentUtils().getRectangleById('itemOne'); this.positionY = infoOne.windowOffset.y + infoOne.size.height + 15; this.positionX = infoOne.windowOffset.x; console.info('ssssss=', JSON.stringify(infoOne)); }, 100); }) .onClick(() => { destroySubWindow(sub1); }); Button('close sub window2') .width((this.screenWidth - 30) / 2) .margin({ left: 10, right: 10 }) .onClick(() => { destroySubWindow(sub2); }); } .margin({ top: 20 }); } .width('100%') .height('100%'); } }
import { destroySubWindow, sub1 } from './Index'; @Entry @Component struct SubWindow { private message: string = 'Hello World'; build() { Column() { Text(this.message) .margin({ top: 10 }) .fontColor(Color.White); Button() { Text('close sub window1') .padding(8) .borderRadius(8) .backgroundColor(Color.Gray) .fontColor(Color.White); } .margin({ top: 10 }) .onClick(() => { destroySubWindow(sub1); }); } .width('100%') .height('100%') // 步骤4:被调用页面实现,可自定义圆角。 .borderRadius(10) .backgroundColor('#ff4b7fe9'); } }
import { destroySubWindow, sub2 } from './Index'; @Entry @Component struct SubWindow2 { private message: string = 'Hello World'; build() { Column() { Text(this.message) .margin({ top: 10 }) .fontColor(Color.White); Button() { Text('close sub window2') .padding(8) .borderRadius(8) .backgroundColor(Color.Gray) .fontColor(Color.White); } .margin({ top: 10 }) .onClick(() => { destroySubWindow(sub2); }); } .width('100%') .height('100%') // 步骤4:被调用页面实现,可自定义圆角。 .borderRadius(10) .backgroundColor('#ff4b7fe9'); } }