HarmonyOS TextArea组件如何主动获取焦点

假设有一个数组,根据数组ForEach循环创建了TextArea组件,如何主动让其中某个TextArea获取焦点?

HarmonyOS
2025-01-09 16:43:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

给TextArea添加defaultFocus(true)即可,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-focus-V5#defaultfocus9

可尝试使用requestFocus:

import promptAction from '@ohos.promptAction';

@Entry
@Component
struct RequestFocusExample {
  @State idList: string[] = ['A', 'B']

  build() {
    Column({ space: 20 }) {
      Row({ space: 5 }) {
        Button("id: " + this.idList[0] + " focusable(false)")
          .width(200).height(70).fontColor(Color.White)
          .id(this.idList[0])
        Button("id: " + this.idList[1])
          .width(200).height(70).fontColor(Color.White)
          .id(this.idList[1])
      }

      Button("RequestFocusA")
        .width(200).height(70).fontColor(Color.White)
        .onClick(() => {
          let res = focusControl.requestFocus(this.idList[0])
          if (res) {
            promptAction.showToast({ message: 'Request success' })
          } else {
            promptAction.showToast({ message: 'Request failed' })
          }
        })
      Button("RequestFocusB")
        .width(200).height(70).fontColor(Color.White)
        .onClick(() => {
          let res = focusControl.requestFocus(this.idList[1])
          if (res) {
            promptAction.showToast({ message: 'Request success' })
          } else {
            promptAction.showToast({ message: 'Request failed' })
          }
        })
    }.width('100%').margin({ top: 20 })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:03:25


相关问题
HarmonyOS 主动获取组件焦点
845浏览 • 1回复 待解决
HarmonyOS TextArea获取焦点
548浏览 • 1回复 待解决
HarmonyOS 主动获取焦点失败
888浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1024浏览 • 1回复 待解决
HarmonyOS Image获取焦点和失去焦点失效
603浏览 • 1回复 待解决
TextInput组件获取焦点的几种场景
3955浏览 • 1回复 待解决