HarmonyOS TextInput在弹框消失后焦点自动获取问题

如果界面上有多个 TextInput 输入控件,类似下面这种:

1、在账号输入中输入内容,点击注册,使用 textController.stopEditing(),让键盘和焦点都消失。

2、点击登录弹出一个弹框。

3、点击button让弹框消失后,账号界面的焦点会自动重新获取。

HarmonyOS
2024-12-26 15:19:54
1168浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可以参考如下demo:

@Entry
@Component
struct TextInputExample {
  @State PassWordSrc1: Resource = $r('app.media.startIcon')
  @State PassWordSrc2: Resource = $r('app.media.background')
  @State TextError: string = ''
  @State Text: string = ''
  @State NameText: string = 'test'
  controller: TextInputController = new TextInputController()
  controller1: TextInputController = new TextInputController()
  @Builder itemEnd() {
    Select([{ value: 'KB' },
      { value: 'MB' },
      { value: 'GB' },
      { value: 'TB', }])
      .height("48vp")
      .borderRadius(0)
      .selected(2)
      .align(Alignment.Center)
      .value('MB')
      .font({ size: 20, weight: 500 })
      .fontColor('#182431')
      .selectedOptionFont({ size: 20, weight: 400 })
      .optionFont({ size: 20, weight: 400 })
      .backgroundColor(Color.Transparent)
      .responseRegion({ height: "40vp", width: "80%", x: '10%', y: '6vp' })
      .onSelect((index: number) => {
        console.info('Select:' + index)
      })
  }
  build() {
    Column({ space: 20 }) {
      // 下划线模式
      TextInput({ controller: this.controller1, placeholder: 'underline style' })
        .showUnderline(true)
        .width(380)
        .height(60)
        .showUnit(this.itemEnd)
      Text(`用户名:${this.Text}`)
        .width('95%')
      TextInput({controller: this.controller, placeholder: '请输入用户名', text: this.Text })
        .showUnderline(true)
        .width(380)
        .showError(this.TextError)
        .onChange((value: string) => {
          this.Text = value
        })
        .onSubmit(() => { // 用户名不正确会清空输入框和用户名并提示错误文本
          if (this.Text == this.NameText) {
            this.TextError = ''
          } else {
            this.TextError = '用户名输入错误'
            this.Text = ''
          }
        })
      Button("点击出现弹窗").width(100).height(100)
        .onClick(() => {
          // 关闭自定义键盘
          this.controller.stopEditing()
          this.controller1.stopEditing()
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              offset: { dx: 0, dy: -20 },
              gridCount: 3,
              confirm: {
                value: 'button',
                action: () => {
                  console.info('Button-clicking callback')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        })
        .backgroundColor(0x317aff)
    }.width('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.
  • 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.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
分享
微博
QQ
微信
回复
2024-12-26 17:16:17
相关问题
HarmonyOS TextInput自动获取焦点问题
810浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1026浏览 • 1回复 待解决
HarmonyOS TextInput焦点问题
1113浏览 • 1回复 待解决
HarmonyOS 输入获取焦点无法弹出
765浏览 • 1回复 待解决
TextInput如何取消自动获得焦点
1325浏览 • 1回复 待解决
HarmonyOS 页面跳转对话消失
890浏览 • 1回复 待解决
TextInput组件获取焦点的几种场景
3973浏览 • 1回复 待解决
获取定位权限没有
2841浏览 • 1回复 待解决
代码获取后台权限?
4301浏览 • 1回复 待解决