diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 631333d05052079c193779de35b8a9b062e64f1c..49f29966ab7c5c0be1410c2aab36b893dde262b1 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -14,6 +14,7 @@ */ import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; @@ -35,10 +36,22 @@ export default class EntryAbility extends UIAbility { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } + this.setWindowClass(windowStage); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); }); } + private setWindowClass(windowStage: window.WindowStage): void { + windowStage.getMainWindow((err: BusinessError, data) => { + const errCode: number = err.code; + if (errCode) { + hilog.info(0x0000, 'testTag', 'Failed to get main window'); + return; + } + AppStorage.setOrCreate('windowClass', data); + }) + } + onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); diff --git a/entry/src/main/ets/pages/BackgroundColorChange.ets b/entry/src/main/ets/pages/BackgroundColorChange.ets index 7863678db3275ac782e1da3c5c4a6e29f0f23deb..6ba8379d22228bd9adf0988cfa33df040be790f0 100644 --- a/entry/src/main/ets/pages/BackgroundColorChange.ets +++ b/entry/src/main/ets/pages/BackgroundColorChange.ets @@ -14,7 +14,9 @@ */ import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import { inputMethod } from '@kit.IMEKit'; +import { window } from '@kit.ArkUI'; import Constants from '../common/Constants'; @Extend(Text) @@ -31,6 +33,8 @@ function verifyCodeUnitStyle() { .borderRadius($r('app.float.row_width')) } +const TAG = 'BackgroundColorChange'; + @Entry @Component struct BackgroundColorChange { @@ -45,13 +49,31 @@ struct BackgroundColorChange { enterKeyType: inputMethod.EnterKeyType.GO } }; + private windowClass: window.Window = AppStorage.get('windowClass') as window.Window; + private isFirstOpenPage: boolean = true; + private registerCount: number = 1; aboutToDisappear(): void { this.detach(); } async attach() { - await this.inputController.attach(true, this.textConfig); + if (this.isFirstOpenPage) { + await this.inputController.attach(true, this.textConfig); + this.listen(); + return; + } + try { + this.windowClass.on('windowEvent', async (windowState) => { + if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { + this.registerCount++; + await this.inputController.attach(true, this.textConfig); + this.listen(); + } + }); + } catch (error) { + hilog.error(0x0000, TAG, `failed to getWindowState callback, error code : ${error.code}`); + } } listen() { @@ -67,9 +89,13 @@ struct BackgroundColorChange { } detach(): void { + this.isFirstOpenPage = false; + this.registerCount = 1; + this.windowClass.off('windowEvent'); this.inputController.off('insertText'); this.inputController.off('deleteLeft'); - this.inputController.detach((_err: BusinessError) => {}); + this.inputController.detach((_err: BusinessError) => { + }); } @Builder @@ -84,8 +110,7 @@ struct BackgroundColorChange { } .onVisibleAreaChange(this.arrArea, async (isVisible: boolean, currentRatio: number) => { if (isVisible && currentRatio >= 1.0) { - await this.attach(); - this.listen(); + this.attach(); } if (!isVisible && currentRatio <= 0.0) { this.detach(); @@ -99,7 +124,7 @@ struct BackgroundColorChange { }) .defaultFocus(true) .onClick(() => { - this.attach(); + this.inputController.attach(true, this.textConfig); }) } diff --git a/entry/src/main/ets/pages/TextBoxShowCursor.ets b/entry/src/main/ets/pages/TextBoxShowCursor.ets index 797024c54933b62db07691edd418f33aed42867b..82772fadaa116eeeb65bed2e467910e402482032 100644 --- a/entry/src/main/ets/pages/TextBoxShowCursor.ets +++ b/entry/src/main/ets/pages/TextBoxShowCursor.ets @@ -14,7 +14,9 @@ */ import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import { inputMethod } from '@kit.IMEKit'; +import { window } from '@kit.ArkUI'; import Constants from '../common/Constants'; @Extend(Text) @@ -31,6 +33,8 @@ function verifyCodeUnitStyle() { }) } +const TAG = 'TextBoxShowCursor'; + @Entry @Component struct TextBoxShowCursor { @@ -47,10 +51,9 @@ struct TextBoxShowCursor { enterKeyType: inputMethod.EnterKeyType.GO } }; - - aboutToAppear(): void { - this.attach(); - } + private windowClass: window.Window = AppStorage.get('windowClass') as window.Window; + private isFirstOpenPage: boolean = true; + private registerCount: number = 1; aboutToDisappear(): void { this.detach(); @@ -60,7 +63,22 @@ struct TextBoxShowCursor { * Binding an Input Method and Subscribing to Input Method-related Events. */ async attach() { - await this.inputController.attach(true, this.textConfig); + if (this.isFirstOpenPage) { + await this.inputController.attach(true, this.textConfig); + this.listen(); + return; + } + try { + this.windowClass.on('windowEvent', async (windowState) => { + if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { + this.registerCount++; + await this.inputController.attach(true, this.textConfig); + this.listen(); + } + }); + } catch (error) { + hilog.error(0x0000, TAG, `failed to getWindowState callback, error code : ${error.code}`); + } } /** @@ -89,6 +107,9 @@ struct TextBoxShowCursor { * Unbinding. */ detach() { + this.isFirstOpenPage = false; + this.registerCount = 1; + this.windowClass.off('windowEvent'); this.inputController.off('insertText'); this.inputController.off('deleteLeft'); this.inputController.detach((_err: BusinessError) => { @@ -119,8 +140,7 @@ struct TextBoxShowCursor { .onVisibleAreaChange(this.arrArea, async (isVisible: boolean, currentRatio: number) => { if (isVisible && currentRatio >= 1) { this.opacityColumn = 1; - await this.attach(); - this.listen(); + this.attach(); } if (!isVisible && currentRatio <= 0) { this.detach(); @@ -137,7 +157,7 @@ struct TextBoxShowCursor { .onClick(() => { // When you click this component, the input method is displayed. Because the Text component is used here, // you need to re-attach it instead of directly using showSoftKeyboard. - this.attach(); + this.inputController.attach(true, this.textConfig); }) Row() { @@ -174,7 +194,7 @@ struct TextBoxShowCursor { }) .hitTestBehavior(HitTestMode.Transparent) .onClick(() => { - this.attach(); + this.inputController.attach(true, this.textConfig); }) .justifyContent(FlexAlign.Center) .height(Constants.ONE_HUNDRED_PERCENT)