Android EditText setError() doesn't work as expected

你离开我真会死。 提交于 2019-12-02 04:13:00

There is a known bug with setError on Jelly Bean_MR1 (4.2 and 4.2.1). I am however assuming that the Nexus 7 you are testing with is running one of those versions of Android. See here: http://code.google.com/p/android/issues/detail?id=40417

The error will be shown while you have focus on that EditText field, but when you lose focus, the error icon is not visible to notify the user of the problem.

Before you set Error on any view or edit text, just call the

yourEditText.requestFocus();
yourEditText.setError("Your Error Message");

then set Error. it will solve your problem. Atleast mine did.

try this

new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        if (editTextIsEmpty(editText) && editText.isEnabled())
            editText.setError("Nodig");
        else
            editText.setError(null);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // nothing here
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // nothing here
    }

}

You can use following code:

May it will be helpful to you:

mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                    com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);

However, you can set a Spanned and a custom error icon using the overloaded setError(CharSequence, Drawable).

You can easily create a Spanned from HTML using fromHtml().

For Example:

yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));

This is the only you need to get expected setError behaviour on the TextView

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!