往期鸿蒙全套实战精彩文章必看内容:
如何一键清空TextInput、TextArea组件内容
将状态变量赋值给TextInput或TextArea组件的text属性,在做点击清空事件时为状态变量赋值空字符串。
参考代码如下:
@Entry
@Component
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%')
}
}