java 如何清除edittext中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7426443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:55:38  来源:igfitidea点击:

How to clear the text in edittext

javaandroidandroid-edittext

提问by anny

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

我想在将文本EditText保存在数据库中后清除其中的文本。

Here is my code:

这是我的代码:

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

  }
}

回答by IgorGanapolsky

Use editText.getText().clear().

使用editText.getText().clear().

回答by Tomzi

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

只需将其设置为空字符串。我认为它应该这样做。

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

回答by Gibolt

If using Kotlin, try:

如果使用 Kotlin,请尝试:

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

回答by Via

you can use EditText.setText("");

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

回答by Ronnie

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

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

回答by bhargavkumar040

String content = textValue.toString();
db.updatePicDes(content,lastSnapId);
editText.setText("");

回答by Balaji.K

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