HarmonyOS TextInputController不支持主动设置焦点

提交表单时,校验非法字段,可能会通过唤起对应组件焦点的方式来提示用户需要修改的字段。

当前组件在失焦状态下,无法通过 controller 达成该能力。

已通过 caretPosition、setTextSelection 方法尝试,它们仅在焦点状态下才能够设置光标状态,并不符合在任何状态都可以调整焦点的预期。

@Entry
@Component
struct Index {
  private stack: NavPathStack = new NavPathStack()
  private ref: TextInputController = new TextInputController()

  // @Builder PageBuilder() {
  //   NavDestination() {
  //     Column() {
  //       TextInput({placeholder: '请输入内容'})
  //         .backgroundColor(Color.Orange)
  //     }.width('100%').height('100%')
  //   }
  // }

  aboutToAppear(): void {
    // this.stack.pushPath({
    //   name: 'page'
    // })
  }

  build() {
    Navigation(this.stack) {
      Stack() {
        Column() {
          Stack({alignContent: Alignment.Center}) {
            TextInput({ placeholder: '请输入内容', text: '', controller: this.ref })
              .backgroundColor(Color.Orange)
          }.width('100%').height('20%')
          Flex() {
            Column() {
              Button("blur")
                .onClick(() => {
                  this.ref.stopEditing()
                })
            }
            Column() {
              Button("focus")
                .onClick(() => {
                  this.ref.caretPosition(0)
                  // this.ref.setTextSelection(0, 0)
                })
            }
          }.width('100%').height('20%')
        }.width('100%')
      }.width('100%').height('60%')
    }
    .width('100%').height('100%')
    // .navDestination(this.PageBuilder)
    .title("NavTitle")
  }
}
  • 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.
HarmonyOS
2024-12-24 17:28:32
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

使用UIContext中的getFocusController()方法获取实例,再通过此实例调用对应方法,能满足当前得诉求。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-focus-V5#focuscontroller12

分享
微博
QQ
微信
回复
2024-12-24 20:29:53
相关问题
Refresh组件不支持设置nestedScroll属性
2719浏览 • 1回复 待解决
HarmonyOS 编码集不支持
764浏览 • 1回复 待解决
HarmonyOS 推送设备不支持
826浏览 • 1回复 待解决
Toggle isOn不支持$$?
801浏览 • 1回复 待解决
web组件不支持localstorage
1496浏览 • 1回复 待解决
HarmonyOS @State不支持HashMap吗
747浏览 • 1回复 待解决
HarmonyOS Span不支持n换行
748浏览 • 1回复 待解决
HarmonyOS image不支持mask吗
822浏览 • 1回复 待解决
HarmonyOS ArkTD不支持any,如何替换
745浏览 • 1回复 待解决
HarmonyOS RN使用datetimePicker显示不支持
761浏览 • 1回复 待解决
HarmonyOS color文件不支持rgba吗
791浏览 • 1回复 待解决
http类不支持cancel方法
886浏览 • 1回复 待解决
HarmonyOS 主动获取焦点失败
884浏览 • 1回复 待解决
HarmonyOS 主动获取组件焦点
844浏览 • 1回复 待解决
HarmonyOS RN不支持相册路径上传
614浏览 • 1回复 待解决
HarmonyOS Text的Span不支持align
696浏览 • 1回复 待解决
HarmonyOS ets不支持匿名类吗?
978浏览 • 2回复 待解决