From 97e5008adf5183d89fbec6ad3b09b3f18ad61b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BC=BA?= <17366354960@163.com> Date: Mon, 2 Dec 2024 16:38:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=80=9C=E9=AA=8C=E8=AF=81=E7=A0=81=E9=9B=86?= =?UTF-8?q?=E5=90=88=E2=80=9D=E5=88=87=E6=8D=A2=E5=BA=94=E7=94=A8=E5=89=8D?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=EF=BC=8C=E9=94=AE=E7=9B=98=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=8B=89=E8=B5=B7=E4=B8=94=E4=B8=8D=E8=83=BD=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 13 +++++++ .../main/ets/pages/BackgroundColorChange.ets | 35 ++++++++++++++--- .../src/main/ets/pages/TextBoxShowCursor.ets | 38 ++++++++++++++----- 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 631333d..49f2996 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 7863678..6ba8379 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 797024c..82772fa 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) -- Gitee