Android 输入后未清除 EditText setError 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11640772/
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
EditText setError message does not clear after input
提问by Michael
Ok so I only have a EditText field and a button, which when pressed triggers an AsyncTask.
好的,所以我只有一个 EditText 字段和一个按钮,按下时会触发 AsyncTask。
EditText playerName = (EditText)findViewById(R.id.playerEditText);
if(playerName.getText().toString().length() == 0 )
playerName.setError("Player name is required!");
else {
// do async task
}
The problem is that the error message seems to stay up even after when I input valid text to search. Is there a way to remove the error as soon as the EditText is not empty?
问题是即使在我输入有效文本进行搜索之后,错误消息似乎仍然存在。一旦 EditText 不为空,有没有办法消除错误?
回答by Alex Curran
In your else bracket, put playerName.setError(null)
, which will clear the error.
在您的 else 括号中, put playerName.setError(null)
,这将清除错误。
回答by gints
API documentation: "The icon and error message will be reset to null when any key events cause changes to the TextView's text." Though it is not so - and therefore we can regard this as bug.
API 文档:“当任何关键事件导致 TextView 的文本发生更改时,图标和错误消息将重置为 null。” 虽然事实并非如此 - 因此我们可以将其视为错误。
If you use inputType such as textNoSuggestions, textEmailAddress, textPassword, the error is unset after a character is typed. Nearly as documented but again not exactly - when you delete a character, error stays. It seems, a simple workaround with addTextChangedListener and setError(null) can attain promised behavior.
如果使用 textNoSuggestions、textEmailAddress、textPassword 等 inputType,则在输入字符后未设置错误。几乎按照文档记录但又不完全 - 当您删除一个字符时,错误仍然存在。看起来,使用 addTextChangedListener 和 setError(null) 的简单解决方法可以实现承诺的行为。
Besides there are posts about icon losing on Android 4.2. So use with care.
此外还有关于 Android 4.2 上图标丢失的帖子。所以请谨慎使用。
回答by Jan Ziesse
Try this listener:
试试这个监听器:
playerName.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt){
if( playerName.getText().length()>0)
{
playerName.setError(null);
}
}
回答by ayush bagaria
If you want to hide the error message one way is you apply onclicklistener on the edit box and then
如果要隐藏错误消息的一种方法是在编辑框上应用 onclicklistener 然后
editTextName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTextName.setError(Null)
}
});
回答by Rissmon Suresh
Below code worked for me
下面的代码对我有用
@OnTextChanged(
value = R.id.editTextName,
callback = OnTextChanged.Callback.TEXT_CHANGED)
public void afterInput(CharSequence sequence) {
editTextName.setError(null);
editTextName.setErrorEnabled(false);
}
'
'
editTextName.setError(null)
Will clear the error message.
editTextName.setErrorEnabled(false)
Will remove additional padding.
editTextName.setError(null)
将清除错误消息。
editTextName.setErrorEnabled(false)
将删除额外的填充。
回答by SureshCS50
Add a TextWatcher to your EditText and onError, show your error message using et.setError(errorMessage)
else you can remove the error message and error icon like below.
将 TextWatcher 添加到您的 EditText 和 onError,使用et.setError(errorMessage)
else显示您的错误消息,您可以删除错误消息和错误图标,如下所示。
// to remove the error message in your EditText
et.setError(null);
// to remove the error icon from EditText.
et.setCompoundDrawables(null, null, null, null);