HarmonyOS 自定义键盘输入框焦点问题

调用controller.stopEditing()输入框失去焦点并且收起了键盘,但是跳转到其他页面返回到当前页面时,输入框自动获取焦点并且弹出了键盘,怎么样才能不自动获取焦点和弹出键盘?

HarmonyOS
2024-12-18 15:07:59
756浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以加下关键性代码,类似于下面按钮上加下 .id(‘333’) 键,然后 focusControl.requestFocus(‘333’) 转移焦点测试下,代码示例可参考:

import router from '@ohos.router';
@Entry
@Component
struct KeyboadPage2 {

  controller: TextInputController = new TextInputController()
  @State inputValue: string = ""
  @State InputBGColor: string = '#90EE90'
  build() {
    Column({ space: 10 }) {
      TextInput({
        controller: this.controller,
      })
        .id('111')
        .backgroundColor(this.InputBGColor)
        .margin(10)
        .border({ width: 1 })
        .height('48vp')
        .onFocus(() => {
          this.InputBGColor = '#FF0000'
        })
        .onBlur(() => {
          this.InputBGColor = '#90EE90'
        })
      Button('收起键盘')
        .onClick(() => {
          setTimeout(() => {
            this.controller.stopEditing()
          }, 0)
        }).id('333')
      Button('push')
        .onClick(() => {
          focusControl.requestFocus('333')
          router.pushUrl({
            url: 'pages/Keyboad/KeyboadPage3',
          })
        })
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
分享
微博
QQ
微信
回复
2024-12-18 16:37:07
相关问题
HarmonyOS 自定义键盘不能顶起输入框
1220浏览 • 1回复 待解决
HarmonyOS 自定义键盘弹起后遮住输入框
1438浏览 • 1回复 待解决
HarmonyOS 修改输入框焦点
724浏览 • 1回复 待解决
HarmonyOS 键盘遮挡输入框
694浏览 • 1回复 待解决
HarmonyOS 输入框屏蔽系统键盘
625浏览 • 1回复 待解决
HarmonyOS 安全键盘输入截屏问题
864浏览 • 1回复 待解决
HarmonyOS 输入框与软键盘问题
802浏览 • 1回复 待解决
HarmonyOS Web输入框键盘mode模式问题
864浏览 • 1回复 待解决
HarmonyOS 键盘输入不能输入负数
526浏览 • 1回复 待解决
HarmonyOS 输入框获取焦点后无法弹出
753浏览 • 1回复 待解决