Android 从先前验证的 EditText 小部件中删除错误指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10206799/
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-08-20 02:52:05  来源:igfitidea点击:

Remove the error indicator from a previously-validated EditText widget

androidandroid-edittext

提问by Arun

I am using an EditText widget, and I am validating it with the setError()method of EditText and it validates correctly.

我正在使用 EditText 小部件,并且我正在使用 EditTextsetError()方法验证它并且它正确验证。

But I have an button in the same screen that redirects to another activity. And when I press back button and come back to the screen the validation still appears.

但是我在同一个屏幕中有一个按钮可以重定向到另一个活动。当我按下后退按钮并返回屏幕时,验证仍然出现。

So on the activity OnPauseevent I want to remove the validation of the EditText. How is it possible.

所以在活动OnPause事件上,我想删除 EditText 的验证。这怎么可能。

回答by Boris Strandjev

protected void onPause () {
    TextView textView = ...; // fetch it as appropriate
    textView.setError(null);
}

Because as mentioned in the documentation:

因为如文档中所述:

If the error is null, the error message and icon will be cleared.

如果错误为空,则错误消息和图标将被清除。

回答by Gibolt

In Kotlin:

在科特林:

editText.error = null

Kotlin Extension Function:

Kotlin 扩展函数:

To make it more readable, you could add this extension function

为了使其更具可读性,您可以添加此扩展功能

fun EditText.clearError() {
    error = null
}

In Java:

在 Java 中:

editText.setError(null);

回答by Aj 27

You can also do it using following :

您也可以使用以下方法进行操作:

protected void onPause () {    
    mEditText.setError(null);//removes error
    mEditText.clearFocus();    //clear focus from edittext
}

回答by Sai Gopi N

just put .setError(null)at the end of the EditText.

只需放在.setError(null)EditText 的末尾。

mEditText.setError(null);

回答by Rahul Raj

In kotlin you can simply acces the property using property access syntax wich is

在 kotlin 中,您可以使用属性访问语法简单地访问属性

protected void onPause () {
    EditText mEditText = ...; // fetch it as appropriate
    mEditText.error = null
}