TextInputLayout设置错误没有消息?

oxc*_*ned 10 android android-textinputlayout

是否可以在文本输入布局上设置错误而不显示任何错误消息(我已在其他地方执行此操作)?

textinputlayout.setError("");
Run Code Online (Sandbox Code Playgroud)

不幸的是不行.

我基本上需要的是textInputLayout将其线条颜色更改为红色,但我需要以编程方式进行.谢谢

Ain*_*ege 10

您可以隐藏带有错误的布局,如下所示:

textinputlayout.setError("");
if (textinputlayout.getChildCount() == 2) {
    textinputlayout.getChildAt(1).setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 7

希望现在还不算太晚,但背后的代码setError是:

if (!mErrorEnabled) {
   if (TextUtils.isEmpty(error)) {
      // If error isn't enabled, and the error is empty, just return
      return;
   }
}
Run Code Online (Sandbox Code Playgroud)

这意味着一个简单的解决方法是:

textinputlayout.setError(" ");

请注意,这将使带有错误消息的 TextView 可见 - 因此即使它是空的,它也会使 TextInputLayout 更高

因为这通过了处理空错误消息请求的不那么深思熟虑的情况。


小智 7

错误文本(甚至“”)将在 TextInputLayout 中占据/保留一些高度。如果你也想摆脱它那么

<com.google.android.material.textfield.TextInputLayout      
    ...
    app:errorTextAppearance="@style/MyZeroSizeTextAppearance"
    ...
    >
Run Code Online (Sandbox Code Playgroud)

文字外观:

<style name="MyZeroSizeTextAppearance">
    <item name="android:textSize">0sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

进而

textinputlayout.setError(" ");
Run Code Online (Sandbox Code Playgroud)