From cbdf06e715b0b9442949db14748b498b90d745d2 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Wed, 17 Sep 2025 11:41:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9EFAQ=E5=90=8C=E6=BA=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/GetFocusByOnVisibleAreaChange.ets | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets diff --git a/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets b/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets new file mode 100644 index 0000000..ba4dbdd --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:设置了动态的visibility属性,切换组件的显示隐藏,使用requestFocus让组件获取焦点报错150003:the component is not on tree or does not exist. +*/ + +// [Start GetFocusByOnVisibleAreaChange] +@Entry +@Component +struct GetFocusByOnVisibleAreaChange { + @State isEdit: boolean = true; + + build() { + Column() { + TextInput().id('input') + .visibility(this.isEdit ? Visibility.Visible : Visibility.none) + .onVisibleAreaChange([1.0], () => { + if (this.isEdit) { + try { + this.getUIContext().getFocusController.requestFocus('input'); + } catch (e) { + console.error('requestFocus error:' + e); + } + } + }) + Button('change visibility') + .onClick(() => { + this.isEdit = !this.isEdit; + }) + } + } +} + +// [End GetFocusByOnVisibleAreaChange] \ No newline at end of file -- Gitee From 3759a7e7cad3317da46be406a12467047541fce6 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Wed, 17 Sep 2025 11:47:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9EFAQ=E5=90=8C=E6=BA=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets b/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets index ba4dbdd..a6db48e 100644 --- a/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets +++ b/ArkUI/entry/src/main/ets/pages/GetFocusByOnVisibleAreaChange.ets @@ -26,11 +26,11 @@ struct GetFocusByOnVisibleAreaChange { build() { Column() { TextInput().id('input') - .visibility(this.isEdit ? Visibility.Visible : Visibility.none) + .visibility(this.isEdit ? Visibility.Visible : Visibility.None) .onVisibleAreaChange([1.0], () => { if (this.isEdit) { try { - this.getUIContext().getFocusController.requestFocus('input'); + this.getUIContext().getFocusController().requestFocus('input'); } catch (e) { console.error('requestFocus error:' + e); } -- Gitee