TextInputLayout's errorview is not removed when removing error message

北慕城南 提交于 2019-12-10 12:38:57

问题


I have a vertical linear layout with some input fields. Using TextInputLayout I get a nice flow with labels and built-in error messages. My problem is when I add and remove the error-messages.

If I add an error message it is positioned below the edit-text and everything looks good.

If I remove the error message with setError(null) the message is removed but the space is still there. This is per googles design apparently(see https://code.google.com/p/android/issues/detail?id=176005). I would very much like this space removed as it makes the UI look very wrong.

If I do .setErrorEnabled(false) the view is removed and everything looks normal again. However if the user changes the data and I do another setError the error-message is not shown (only the edit text line is red).


回答1:


As of Support library version 23.1.1 (and perhaps earlier), this should no longer be the case. You should be able to call TextInputLayout.setErrorEnabled(false) to hide the error TextView and calling TextInputLayout.setError(error) now internally calls TextInputLayout.setErrorEnabled(true) if the error isn't null or empty. See the code snippet below, taken from the support library:

public void setError(@Nullable CharSequence error) {
    if (!mErrorEnabled) {
        if (TextUtils.isEmpty(error)) {
            // If error isn't enabled, and the error is empty, just return
            return;
        }
        // Else, we'll assume that they want to enable the error functionality
        setErrorEnabled(true);
    }
    ...
}



回答2:


For me, below code is working fine.

@Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(mobileNoInputLayout.isErrorEnabled()){
            mobileNoInputLayout.setErrorEnabled(false);
        }
    }


来源:https://stackoverflow.com/questions/33568952/textinputlayouts-errorview-is-not-removed-when-removing-error-message

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