在HarmonyOS NEXT开发中自定义键盘输入框焦点问题?

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

阅读 579
1 个回答

可以加下关键性代码,类似于下面按钮上加下 .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%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进