繁体   English   中英

如何清除edittext中的文本

[英]How to clear the text in edittext

我想在EditText的文本保存到数据库后清除它。

这是我的代码:

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

  }
}

使用editText.getText().clear()

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

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

如果使用 Kotlin,请尝试:

editText.text.clear()

扩展解决方案

或者,您可以添加一个小的扩展功能,使其更加简单。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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