开发者

How to clear the text in edittext

开发者 https://www.devze.com 2023-04-05 12:54 出处:网络
I want to clear the text in EditText after it has been saved in database. Here i开发者_开发知识库s my code:

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

Here i开发者_开发知识库s my code:

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

  }
}


Use editText.getText().clear().


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()
}


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

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


you can use EditText.setText("");


You better do that in FocusChangedListener if you donot want it on clicking a button.


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("");
0

精彩评论

暂无评论...
验证码 换一张
取 消