HarmonyOS 主动获取焦点失败

通过focusControl.requestFocus让已经设置id的TextInput获取焦点,但返回失败

页面中的TextInput通过代码主动获取焦点

let res = focusControl.requestFocus('login_verify_code_input') // 使TextInput获焦  
if (res) {  
  Toast.showToast('success');  
} else {  
  Toast.showToast('failed');  
}  
  
TextInput()  
  .onChange((value) => {  
  })  
  .maxLength(CommonConstants.INPUT_VERIFY_LENGTH)  
  .type(InputType.Number)  
  .backgroundColor(Color.Transparent)  
  .id('login_verify_code_input')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
HarmonyOS
2024-10-21 10:59:08
1507浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

由开发文档中可以看到:ocusControl.requestFocus所接收的参数为组件.key中的值而非id

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-focus-V5#requestfocus9

demo更改为:

TextInput()  
.onChange((value) => {  
})  
.maxLength(CommonConstants.INPUT_VERIFY_LENGTH)  
.type(InputType.Number)  
.backgroundColor(Color.Transparent)  
.key('login_verify_code_input')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
分享
微博
QQ
微信
回复
2024-10-21 17:21:27
相关问题
HarmonyOS 主动获取组件焦点
840浏览 • 1回复 待解决
HarmonyOS TextArea组件如何主动获取焦点
555浏览 • 1回复 待解决
HarmonyOS Image获取焦点和失去焦点失效
599浏览 • 1回复 待解决
HarmonyOS TextArea获取焦点
538浏览 • 1回复 待解决
HarmonyOS 获取焦点api提示异常
723浏览 • 1回复 待解决
HarmonyOS TextInput自动获取焦点问题
801浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1015浏览 • 1回复 待解决
HarmonyOS获取签名信息获取失败
952浏览 • 1回复 待解决
HarmonyOS获取NativeXcomponent失败
1334浏览 • 1回复 待解决
HarmonyOS OAID获取失败
1549浏览 • 2回复 待解决