简体   繁体   中英

How to clear the text in edittext

I want to clear the text in EditText after it has been saved in database.

Here is my code:

pictureDescription.addTextChangedListener(new TextWatcher()
{
  public void afterTextChanged(Editable textValue)
  {
    String content = textValue.toString();
    db.updatePicDes(content,lastSnapId);

  }
}

使用editText.getText().clear()

Just set it to empty string. I think it should do it.

EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("");

If using Kotlin, try:

editText.text.clear()

Extension Solution

Alternatively, you can add a small extension function to make it even simpler.

editText.clear()

fun EditText.clear() {
    text.clear()
}

你可以使用EditText.setText("");

如果您不想在单击按钮时执行此操作,则最好在 FocusChangedListener 中执行此操作。

pictureDescription.addTextChangedListener(new TextWatcher() {
   public void  afterTextChanged(Editable textValue) { 
          String content = textValue.toString(); 
          db.updatePicDes(content,lastSnapId);
           pictureDescription.setText("");
   }
}
String content = textValue.toString();
db.updatePicDes(content,lastSnapId);
editText.setText("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM