Android TextInputLayout setErrorEnabled不';无法创建新的TextView对象
我在创建登录表单时发现一个问题。当用户正确填写内容时,我会在TextInputLayout上显示一些错误并禁用它们 我把它放在Android TextInputLayout setErrorEnabled不';无法创建新的TextView对象,android,layout,textview,android-textinputlayout,Android,Layout,Textview,Android Textinputlayout,我在创建登录表单时发现一个问题。当用户正确填写内容时,我会在TextInputLayout上显示一些错误并禁用它们 我把它放在 mTextInputLayout.setError("This field is required"); 并使用 mTextInputLayout.setError(null); 问题是仍然存在显示错误消息的空TextView对象的填充。因此,我尝试通过设置完全禁用错误 mTextInputLayout.setErrorEnabled(false); 它工作正常
mTextInputLayout.setError("This field is required");
并使用
mTextInputLayout.setError(null);
问题是仍然存在显示错误消息的空TextView对象的填充。因此,我尝试通过设置完全禁用错误
mTextInputLayout.setErrorEnabled(false);
它工作正常,看起来很好,但我不能再打开它了。打电话的时候
mTextInputLayout.setErrorEnabled(true);
mTextInputLayout.setError("This field is required");
再一次,我只看到一个读取行,而不是错误消息,因此显示错误消息的TextView似乎已被销毁,不再创建
我读到,当调用setErrorEnabled(false)
时,TextView对象会被销毁,并且似乎不会再次创建它。Bug还是特性
这个控件的源代码在AOSP中还不可用,所以我使用Android Studio内置的反编译器来检查代码,以了解出了什么问题。我发现setErrorEnabled()实际上创建和销毁了TextView对象,而我希望它只是切换可见性
如果有人面临同样的问题,我会找到一个很好的解决办法。 只需打开和关闭错误TextView对象的可见性,不要破坏该对象 使用此选项可启用错误消息:
if (textInputLayout.getChildCount() == 2)
textInputLayout.getChildAt(1).setVisibility(View.VISIBLE);
textInputLayout.setError("This field is required");
textInputLayout.setError(null);
if (textInputLayout.getChildCount() == 2)
textInputLayout.getChildAt(1).setVisibility(View.GONE);
此选项用于禁用错误消息:
if (textInputLayout.getChildCount() == 2)
textInputLayout.getChildAt(1).setVisibility(View.VISIBLE);
textInputLayout.setError("This field is required");
textInputLayout.setError(null);
if (textInputLayout.getChildCount() == 2)
textInputLayout.getChildAt(1).setVisibility(View.GONE);
从支持库版本23.1.1(可能更早)起,调用setErrorEnabled(false)将删除错误TextView,并在随后调用setError(String)时导致TextInputLayout显示新错误 但是,仍然存在一个缺陷,即一旦清除错误消息,就不会从布局中删除额外的填充。通过使用上面@dabo的帖子,可以解决此错误:
在我的案例中,设置错误、清除错误和设置错误再次导致错误。一条线没有再次变为红色(API 23.4.0)。该解决方案有助于:
调用
setErrorEnabled(false)
在setError(null)
谢谢,我也遇到了同样的问题。我不知道谷歌为什么要这样实现,但是setErrorEnabled(true)不能像预期的那样工作,这很可怕->如果我们在我面对java.lang.NoClassDefFoundError之前调用setErrorEnabled(false),setErrorEnabled(true)之后错误不可见:未能解决:Landroid/support/v7/internal/widget/TintManager;位于android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:379),位于TextInputLayout.setError(null);您正在将android支持库导入项目(在build.gradle文件中)?依赖项{compile'com.android.support:design:23.1.0'}textInputLayout.getChildAt(1)可能返回错误的视图,如果您同时使用计数器。