diff --git a/entry/src/main/ets/common/constants/Constants.ets b/entry/src/main/ets/common/constants/Constants.ets index 83341f1..6685ada 100644 --- a/entry/src/main/ets/common/constants/Constants.ets +++ b/entry/src/main/ets/common/constants/Constants.ets @@ -139,6 +139,22 @@ export class CommonConstants { static readonly LINK_DATA: Link[] = [ new Link("第二页","pages/Second"), new Link("方法页","pages/Method"), - new Link("待办页","pages/ToDoListPage") + new Link("待办页","pages/ToDoListPage"), + new Link("生命周期","") ]; + + /** + * Default fontSize + */ + static DEFAULT_FONT_SIZE: number = 30; + + /** + * The text width + */ + static DEFAULT_MARGIN: number = 30; + + /** + * Full the width. + */ + static FULL_WIDTH: string = '100%'; } \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 8335b76..bb91f7b 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -3,39 +3,59 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; export default class EntryAbility extends UIAbility { + domain: number = 0x0000; + + windowStageEventFunc: (data: window.WindowStageEventType) => void = (data: window.WindowStageEventType): void => { + hilog.info( + this.domain, + 'Succeeded in enabling the listener for window stage event changes. Data: %{public}', + JSON.stringify(data) ?? '' + ); + } + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { + // 设置WindowStage事件订阅(获取/失焦、可见/不可见) + try { + windowStage.on('windowStageEvent', this.windowStageEventFunc); + } catch (exception) { + hilog.error( + this.domain, + 'Failed to enable the listener for window stage event changes. Cause: %{public}', + JSON.stringify(exception) ?? '' + ); + } // Main window is created, set main page for this ability - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + hilog.error(this.domain, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + hilog.info(this.domain, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onBackground'); } } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 21ad713..f40ac82 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -8,7 +8,7 @@ import DataModel from '../viewmodel/DataModel'; @Entry @Component struct Index { - @State message: string = 'Hello World' + @State message: string = 'HarmonyOS第一课学习' private linkData: Link[] = []; @@ -20,22 +20,22 @@ struct Index { Row() { Column() { Text(this.message) - .fontSize(50) + .fontSize(30) .fontWeight(FontWeight.Bold) // 添加按钮,以响应用户点击 ForEach(this.linkData, (link: Link) => { Button() { Text(link.name) - .fontSize(30) + .fontSize(24) .fontWeight(FontWeight.Bold) } .type(ButtonType.Capsule) .margin({ - top: 20 + top: 10 }) .backgroundColor('#0D9FFB') - .width('40%') + .width('80%') .height('5%') .onClick(() => { console.info(`Succeeded in clicking the 'Next' button.`) diff --git a/entry/src/main/ets/pages/LifeCyclePage.ets b/entry/src/main/ets/pages/LifeCyclePage.ets new file mode 100644 index 0000000..cb983e0 --- /dev/null +++ b/entry/src/main/ets/pages/LifeCyclePage.ets @@ -0,0 +1,43 @@ +import Logger from '../common/utils/Logger'; +import { CommonConstants } from '../common/constants/Constants'; + +@Entry +@Component +struct LifeCyclePage { + @State textColor: Color = Color.Black; + + aboutToAppear() { + this.textColor = Color.Blue; + Logger.info('[LifeCyclePage] LifeCyclePage aboutToAppear'); + } + + onPageShow() { + this.textColor = Color.Brown; + Logger.info('[LifeCyclePage] LifeCyclePage onPageShow'); + } + + onPageHide() { + Logger.info('[LifeCyclePage] LifeCyclePage onPageHide'); + } + + onBackPress() { + this.textColor = Color.Red; + Logger.info('[LifeCyclePage] LifeCyclePage onBackPress'); + return false; + } + + aboutToDisappear() { + Logger.info('[LifeCyclePage] LifeCyclePage aboutToDisappear'); + } + + build() { + Column() { + Text($r('app.string.hello_message')) + .fontSize(CommonConstants.DEFAULT_FONT_SIZE) + .fontColor(this.textColor) + .margin(CommonConstants.DEFAULT_MARGIN) + .fontWeight(FontWeight.Bold) + } + .width(CommonConstants.FULL_WIDTH) + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index efda7fd..424558e 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -59,6 +59,10 @@ { "name": "page_title", "value": "待办" + }, + { + "name": "hello_message", + "value": "Hello World" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index ad752d7..dc434db 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -3,6 +3,7 @@ "pages/Index", "pages/Second", "pages/Method", - "pages/ToDoListPage" + "pages/ToDoListPage", + "pages/LifeCyclePage" ] } diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index fd35254..1b6ecc9 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -59,6 +59,10 @@ { "name": "page_title", "value": "ToDo" + }, + { + "name": "hello_message", + "value": "Hello World" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 563f1af..2a2dadc 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -59,6 +59,10 @@ { "name": "page_title", "value": "待办" + }, + { + "name": "hello_message", + "value": "你好世界" } ] } \ No newline at end of file