diff --git a/entry/src/main/ets/pages/MainPage.ets b/entry/src/main/ets/pages/MainPage.ets index 0195d7acbf67ae3f924e71e9e5b3a3cb09f2813f..9c0b128b50cec83da31d56180c6e8d61170dd7a2 100644 --- a/entry/src/main/ets/pages/MainPage.ets +++ b/entry/src/main/ets/pages/MainPage.ets @@ -22,6 +22,7 @@ import { TextInputComponent } from '../view/TextInputComponent'; @Component struct MainPage { @State bottomPadding: number = 210; + @State isKeyboardShown: boolean = false; aboutToAppear(): void { let event: emitter.InnerEvent = { @@ -49,7 +50,7 @@ struct MainPage { .borderRadius(12) .padding({ left: $r('app.float.main_page_margin'), right: $r('app.float.main_page_margin') }) - TextInputComponent() + TextInputComponent({ isKeyboardShown: this.isKeyboardShown }) // [EndExclude MainPage_start] } .padding({ bottom: this.bottomPadding }) @@ -59,6 +60,11 @@ struct MainPage { .expandSafeArea([SafeAreaType.KEYBOARD, SafeAreaType.SYSTEM]) // [EndExclude MainPage_start] } + .onClick(() => { + if (this.isKeyboardShown) { + this.isKeyboardShown = false; + } + }) .mode(NavigationMode.Stack) .titleMode(NavigationTitleMode.Full) .title($r('app.string.main_page_title')) diff --git a/entry/src/main/ets/view/TextInputComponent.ets b/entry/src/main/ets/view/TextInputComponent.ets index 3e043ca2c610aabe3829ca99c92fa4c8040df88e..da4703a0f4d82b6d42d5d29ad35111ef27a6330a 100644 --- a/entry/src/main/ets/view/TextInputComponent.ets +++ b/entry/src/main/ets/view/TextInputComponent.ets @@ -29,6 +29,7 @@ export struct TextInputComponent { @Provide systemKeyboardHeight: number = 0; @Provide customKeyboardHeight: number = 0; @Provide bottomRectHeight: number = 0; + @Link @Watch('onChangeKeyboard') isKeyboardShown: boolean; textInputController: TextInputController = new TextInputController(); aboutToAppear(): void { @@ -47,6 +48,13 @@ export struct TextInputComponent { }) }) } + + onChangeKeyboard() { + if (this.isKeyboardShown === false) { + this.textInputController.stopEditing(); + } + } + // [EndExclude TextInputComponent_start] // [Start customKeyboard_start] build() { @@ -79,10 +87,12 @@ export struct TextInputComponent { // [StartExclude TextInput_start] .onBlur(() => { this.isTabViewShow = false; + this.isKeyboardShown = false; this.keyBoardController.changeAvoidHeight(0); }) .onFocus(() => { this.isTabViewShow = true; + this.isKeyboardShown = true; }) .onChange((value) => { this.inputText = value;