HarmonyOS 界面中多个textInput,如何指定某一个主动获取焦点,非首次进入情况

HarmonyOS
2024-12-24 15:56:59
652浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

可以通过 focusControl.requestFocus指定组件获取焦点,以下为代码例:

import promptAction from '@ohos.promptAction';

@Entry
@Component
struct RequestFocusExample {

  @State selectId: string = 'A'

  build() {
    Column({ space:20 }){
      Row({space: 5}) {

        TextInput({placeholder:'请输入1。。。'})
          .width(80).height(70).fontColor(Color.White)
          .key('A')
        TextInput({placeholder:'请输入2。。。'})
          .width(80).height(70).fontColor(Color.White)
          .key('B')
        TextInput({placeholder:'请输入3。。。'})
          .width(80).height(70).fontColor(Color.White)
          .key('C')
        TextInput({placeholder:'请输入4。。。'})
          .width(80).height(70).fontColor(Color.White)
          .key('D')
      }

      Row({space: 5}) {
        Button("A-RequestFocus")
          .width(80).height(70).fontColor(Color.White)
          .onClick(() => {
            let res = focusControl.requestFocus('A')      // 使选中的this.selectId的组件获焦
            if (res) {
              promptAction.showToast({message: 'Request success'})
            } else {
              promptAction.showToast({message: 'Request failed'})
            }
          })
        Button("B-RequestFocus")
          .width(80).height(70).fontColor(Color.White)
          .onClick(() => {
            let res = focusControl.requestFocus('B')      // 使选中的this.selectId的组件获焦
            if (res) {
              promptAction.showToast({message: 'Request success'})
            } else {
              promptAction.showToast({message: 'Request failed'})
            }
          })
        Button("C-RequestFocus")
          .width(80).height(70).fontColor(Color.White)
          .onClick(() => {
            let res = focusControl.requestFocus('C')      // 使选中的this.selectId的组件获焦
            if (res) {
              promptAction.showToast({message: 'Request success'})
            } else {
              promptAction.showToast({message: 'Request failed'})
            }
          })
        Button("DS-RequestFocus")
          .width(80).height(70).fontColor(Color.White)
          .onClick(() => {
            let res = focusControl.requestFocus('D')      // 使选中的this.selectId的组件获焦
            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.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
分享
微博
QQ
微信
回复
2024-12-24 18:27:32
相关问题
Scroll中点击某一个层图片移动到顶端
1388浏览 • 1回复 待解决
HarmonyOS 主动获取组件焦点
839浏览 • 1回复 待解决
HarmonyOS 主动获取焦点失败
880浏览 • 1回复 待解决
HarmonyOS TextArea组件如何主动获取焦点
555浏览 • 1回复 待解决