TextInput组件如何清空输入框内输入的内容

搜索输入框点击搜索按钮后需要清空输入框中的内容。

HarmonyOS
2024-09-30 10:34:47
浏览
收藏 0
回答 3
待解决
回答 3
按赞同
/
按时间
put_get

demo例子:

struct Index {  
  @State text: string = 'Hello World'  
  controller: TextInputController = new TextInputController()  
  build() {  
    Row() {  
      Column() {  
        TextInput({ placeholder: 'Please input your words.', text: this.text,  
          controller:this.controller}).onChange((value) => {  
          this.text = value  
        })  
        Button("Clear TextInput").onClick(() => {  
          this.text = "";  
        })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-09-30 17:54:19
wei_shuo

清空 ​TextInput​ 输入框,可借助 ​@State​ 定义状态变量并绑定到 ​TextInput​ 的 ​value​ 属性,通过 ​onInput​ 事件更新状态,点击清空按钮时把状态变量重置为空字符串

分享
微博
QQ
微信
回复
2025-03-23 13:19:06
wei_shuo

清空 TextInput 输入框内容,可通过双向数据绑定,用​@State​定义状态变量绑定到 TextInput 的​value​属性,在清空逻辑处将该状态变量设为空字符串,如点击清空按钮触发事件里执行此操作,从而达成清空目的

感谢,望采纳

已于2025-3-23 13:22:08修改
分享
微博
QQ
微信
回复
2025-03-23 13:21:57
相关问题
HarmonyOS textInput 如何清空输入框
1245浏览 • 2回复 待解决
如何清空文本输入框内容
2179浏览 • 2回复 待解决
js如何清空一个input 输入框内容
8858浏览 • 2回复 待解决
获取输入框输入内容
875浏览 • 2回复 待解决
HarmonyOS 输入框不显示内容
1490浏览 • 2回复 待解决
HarmonyOS TextInput输入内容限制
1889浏览 • 1回复 待解决
HarmonyOS TextInput如何阻止其输入内容
1521浏览 • 1回复 待解决