示例参考// 1.EntryAbility.ets import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; import { BusinessError } from '@ohos.base'; let sub_windowClass:window.Window |null = null; export default class EntryAbility extends UIAbility { destroySubWindow() { // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。 sub_windowClass?.destroyWindow((err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); return; } console.info('Succeeded in destroying the window.'); }); } onWindowStageCreate(windowStage: window.WindowStage) { AppStorage.SetOrCreate('window', windowStage); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); return; } console.error('Succeeded in loading the main page content.'); }); } onWindowStageDestroy() { // this.destroySubWindow(); } }; // 2.Index.ets // Index.ets import window from '@ohos.window'; import router from '@ohos.router'; import { BusinessError } from '@ohos.base'; let windowStage_: window.WindowStage | null = null; @Entry @Component struct Index { @State message: string = 'DEMO for EyeCare'; @StorageLink('window') storWindow: window.WindowStage|null = null; private windowStage = this.storWindow; private sub_windowClass: window.Window|null = null; showSubWindow() { // 0.获取当前系统的window显示设置 // let windowLimits = windowClass.getWindowLimits(); // let maxWinWidth = windowLimits?.maxWidth; // let maxHeight = windowLimits?.maxHeight; // console.info(`Get the window limits: maxWinWidth:${maxWinWidth}, maxHeight:${maxHeight}`); // 1.创建应用子窗口。 this.windowStage?.createSubWindow("maskWindow", (err: BusinessError, data) => { let errCode: number = err.code; if (errCode) { console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); return; } this.sub_windowClass = data; console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。 this.sub_windowClass?.moveWindowTo(0, 0, (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to move the window. Cause:' + JSON.stringify(err)); return; } console.info('Succeeded in moving the window.'); }); this.sub_windowClass?.resize(1260, 2720, (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); return; } console.info('Succeeded in changing the window size.'); }); // 3.为子窗口加载对应的目标页面。 this.sub_windowClass?.setUIContent("pages/subWindow", (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to load the content. Cause:' + JSON.stringify(err)); return; } (this.sub_windowClass as window.Window).setWindowBackgroundColor('#33e3ffcd');//33e3edcd (this.sub_windowClass as window.Window).setWindowFocusable(false); (this.sub_windowClass as window.Window).setWindowTouchable(false, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error(`Failed to set the window to be touchable. Cause code: ${err.code}, message: ${err.message}`); return; } console.info('Succeeded in setting the window to be touchable.'); }); console.info('Succeeded in loading the content.'); // 3.显示子窗口。 (this.sub_windowClass as window.Window).showWindow((err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); return; } console.info('Succeeded in showing the window.'); }); }); }) } destroySubWindow() { // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。 (this.sub_windowClass as window.Window).destroyWindow((err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); return; } console.info('Succeeded in destroying the window.'); }); } build() { Row() { Column() { Text(this.message) .fontSize(24) .fontWeight(FontWeight.Bold) Button() { Text('Next Page') .fontSize(20) .fontWeight(FontWeight.Bold) } .type(ButtonType.Capsule) .margin({ top: 20 }) .backgroundColor('#0D9FFB') .width('40%') .height('5%') // 跳转按钮绑定onClick事件,点击时跳转到第二页 .onClick(() => { console.info(`Succeeded in clicking the 'Next' button.`) // 跳转到第二页 router.pushUrl({ url: 'pages/Second' }).then(() => { console.info('Succeeded in jumping to the second page.') }).catch((err: BusinessError) => { console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`) }) }) Button(){ Text('CreateSubWindow') .fontSize(24) .fontWeight(FontWeight.Normal) }.width(220).height(68) .margin({left:10, top:60}) .onClick(() => { // this.CreateSubWindow() this.showSubWindow(); }) Button(){ Text('destroySubWindow') .fontSize(24) .fontWeight(FontWeight.Normal) }.width(220).height(68) .margin({left:10, top:60}) .onClick(() => { this.destroySubWindow() }) } .width('100%') } .height('100%') } } // 3 Second.ets // Second.ets import { router } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Second { @State message: string = 'this is second page'; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) Button() { Text('Back') .fontSize(25) .fontWeight(FontWeight.Bold) } .type(ButtonType.Capsule) .margin({ top: 20 }) .backgroundColor('#0D9FFB') .width('40%') .height('5%') // 返回按钮绑定onClick事件,点击按钮时返回到第一页 .onClick(() => { console.info(`Succeeded in clicking the 'Back' button.`) try { // 返回第一页 router.back() console.info('Succeeded in returning to the first page.') } catch (err) { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`) } }) } .width('100%') } .height('100%') } } // 4.SubWindow.ets // subWindow.ets @Entry @Component struct SubWindow { @State message: string = 'subWindow: untouchable & unfocusable'; build() { Row() { Column() { Text(this.message) .fontSize(12) .fontWeight(FontWeight.Bold) } .width('100%') } .backgroundColor('#33e3ffcd') .height('100%') } }
示例参考