TextInputLayout setErrorEnabled doesn't create new TextView object

我是研究僧i 提交于 2019-12-04 02:41:11

In case someone faces the same problem, I found a workaround that works fine. Just set the visibility of the error TextView object on and off, don't destroy the object.

Use this for enabling the error message:

if (textInputLayout.getChildCount() == 2)
    textInputLayout.getChildAt(1).setVisibility(View.VISIBLE);

textInputLayout.setError("This field is required");

and this for disabling the error message:

textInputLayout.setError(null);

if (textInputLayout.getChildCount() == 2)
    textInputLayout.getChildAt(1).setVisibility(View.GONE);

As of Support library version 23.1.1 (and perhaps earlier), calling setErrorEnabled(false) will remove the error TextView and cause the TextInputLayout to display a new error when setError(String) is subsequently called.

However, there is still an existing bug where additional padding is not removed from the Layout once the error message is cleared. This bug can be worked around by using @dabo's post above:

https://code.google.com/p/android/issues/detail?id=200137

CoolMind

In my case setting error, clearing error and setting error again caused a bug. A line hasn't become red again (API 23.4.0). This solution helped: TextInputLayout.setError() leaves empty space after clearing the error

Call setErrorEnabled(false) after setError(null).

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