目录

How do I clear the content of the TextInput and TextArea components with one click?

Bind the text attribute of the TextInput or TextArea component to a state variable. Assign an empty string to the state variable when you click the clear button, and then the content is cleared. The code snippet is as follows:

@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%')
  }
}