HarmonyOS TextInput修改输入内容?

我在使用TextInput输入框时候,有这样一个需求,比如我点击一个按钮时候,需要重新设置输入框里面的默认内容或者清空里面的输入内容。

我看了下api没有找到适合的方法,比如我尝试定义

@State textInputText:string=''
build() {
  TextInput({text:this.textInputText,controller: this.controller })
    .id('ti_input_answer')
    .type(InputType.NUMBER_DECIMAL)//带小数点的小数输入模式
    .width('70%')
    .height(40)
    .maxLength(20)
    .margin({ left: '5%' })
    .onChange((value: string) => {
      this.userInputAnswer = value
    })
}

然后再点击按钮时候改变textInputText 的值。但是TextInput里面的值还是没改变。

请教下实现方法?

阅读 507
1 个回答

请参考一下示例,

@Entry
@Component
struct CustomDialogUser {
  @State textInputText:string=''
  controller:TextInputController = new TextInputController()
dialogController置空
  aboutToDisappear() {

  }
  build() {
    Column() {
      TextInput({text:this.textInputText,controller: this.controller })
        .id('ti_input_answer')
        .type(InputType.NUMBER_DECIMAL)
        .width('70%')
        .height(40)
        .maxLength(20)
        .margin({ left: '5%' })
        .onChange((value: string) => {
          this.textInputText = value
        })

      Button('click me')
        .onClick(() => {
            this.textInputText = ''
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}