怎样通让已失焦的TextInput重新获取焦点并弹出键盘

怎样通让已失焦的TextInput重新获取焦点并弹出键盘


HarmonyOS
2024-02-20 10:55:34
3308浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
wjmfzsz

可以使用sendEventByKey使输入框触发点击事件。

// Index.ets 
import promptAction from '@ohos.promptAction'; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  @State isBoolean: boolean = true; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .id('1sa') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Button('我会获取焦点') 
          .key('BBB') 
        TextInput({ placeholder: 'AAA' }) 
          .id('AAA') 
          .type(InputType.Normal) 
        Button('点击焦点转移') 
          .onClick(() => { 
            let res = focusControl.requestFocus('BBB'); 
            if (res) { 
              promptAction.showToast({ message: 'Request success' }); 
            } else { 
              promptAction.showToast({ message: 'Request failed' }); 
            } 
          }) 
        Button('点击获取焦点') 
          .onClick(() => { 
            this.isBoolean = true; 
            sendEventByKey('AAA', 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.
  • 41.

已于2024-2-21 18:34:09修改
分享
微博
QQ
微信
回复
2024-02-20 19:57:10


相关问题
HarmonyOS 如何TextInput获得输入焦点
852浏览 • 1回复 待解决
HarmonyOS textinput键盘弹出问题
1387浏览 • 1回复 待解决
HarmonyOS TextInput如何主动弹出键盘
1116浏览 • 1回复 待解决
TextInput组件获取焦点几种场景
3988浏览 • 1回复 待解决
怎样用户选择加载图片
9931浏览 • 1回复 待解决
HarmonyOS TextInput自动获取焦点问题
810浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1034浏览 • 1回复 待解决