HarmonyOS TextInput自动获取焦点问题

页面上上下排列了一个TextInput和一个按钮,点击 按钮会使用CustomDialogController弹窗,关闭弹窗后,TextInput自动获取焦点····,怎么不让他自动获取焦点,设置了.defaultFocus(false).groupDefaultFocus(false)都没用

HarmonyOS
2024-12-18 17:01:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可使用requestFocus 方式,下面是相关demo:

import prompt from '@ohos.prompt';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State isboole: boolean = true;

  build() {
    Row() {
      Column() {
        Text(this.message)
          .id('1sa')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('我会获取焦点')
          .key('BBB')
        TextInput({ placeholder: 'AAAAA' })
          .id('AAAA1')
          .type(InputType.Normal)
        Button('点击焦点转移')
          .onClick(() => {
            let res = focusControl.requestFocus('BBB')
            if (res) {
              prompt.showToast({ message: 'Request success' })
            } else {
              prompt.showToast({ message: 'Request failed' })
            }
          })
        Button('点击获取焦点')
          .onClick(() => {
            this.isboole = true
            sendEventByKey("AAAA1", 10, "") // 向id为"longClick"的组件发送点击事件
          })
      }
      .width('100%')
    }
    .height('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.
分享
微博
QQ
微信
回复
2024-12-18 18:16:45
相关问题
HarmonyOS TextInput组件无法自动获取焦点
1017浏览 • 1回复 待解决
HarmonyOS TextInput焦点问题
1108浏览 • 1回复 待解决
TextInput如何取消自动获得焦点
1303浏览 • 1回复 待解决
TextInput组件获取焦点的几种场景
3948浏览 • 1回复 待解决
HarmonyOS TextInput 取消默认焦点
1108浏览 • 1回复 待解决
HarmonyOS TextInput无法取消焦点
938浏览 • 1回复 待解决
HarmonyOS 如何让TextInput获得输入焦点
832浏览 • 1回复 待解决
如何监听TextInput是否获得焦点
2565浏览 • 1回复 待解决