HarmonyOS TextInput双向绑定无效

使用$$进行变量的双向绑定,点击输入框并在onFocus回调中将变量置空。此时若点击的位置为输入框文字的末尾则置空生效,若点击的是文字的中间部分则置空无效。无效表现为在赋值为空后变量内容会变为空,但在短时间内又会恢复为之前的输入框内的内容。

HarmonyOS
2025-01-09 15:41:18
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

加上TextInput组件的控制器,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-two-way-sync-V5

参考示例如下:

const listArr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

export class CommonConstants {
  static readonly TAB_NAME_1: string = 'Tab1';
  static readonly TAB_NAME_2: string = 'Tab2';
  static readonly LIST_NAME_1: string = 'Tab1_List';
  static readonly LIST_NAME_2: string = 'Tab2_List';
  static readonly FONT_WEIGHT_FIVE: number = 500;
  static readonly FONT_WEIGHT_FOUR: number = 400;
  static readonly STROKE_WIDTH: number = 2;
  static readonly LIST_SPACE: number = 12;
  static readonly STACK_HEIGHT: Length = "calc(100% - 60vp)";
  static readonly FULL_OPACITY: number = 1;
  static readonly ZERO_OPACITY: number = 0;
  static readonly FULL_WIDTH: string = '100%';
  static readonly FULL_HEIGHT: string = '100%';
}


@Entry
@Component
struct TextInputPage {
  @State message: string = 'Hello World';
  @State other: string = '123'
  controller: TextInputController = new TextInputController()

  build() {
    Column() {
      TextInput({ text: this.message, placeholder: '请输入', controller: this.controller })
        .onFocus(() => {
          this.message = ''
          console.log('message:' + this.message)
          setTimeout(() => {
            console.log('message:' + this.message)
          }, 10)
        })

      TextInput({ text: $$this.other, placeholder: '请输入' })
    }.justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
2025-01-09 18:58:32
相关问题
HarmonyOS $$双向绑定问题
707浏览 • 1回复 待解决
HarmonyOS 无法使用$$双向绑定
1414浏览 • 1回复 待解决
HarmonyOS如何实现双向数据绑定
1040浏览 • 1回复 待解决
ArkTS简单类型变量双向数据绑定
2150浏览 • 1回复 待解决
HarmonyOS TextInput绑定自定义键盘问题
966浏览 • 1回复 待解决
字母表与侧标滚动栏的双向绑定
1010浏览 • 1回复 待解决