本文介绍了删除错误消息时,不会删除TextInputLayout的errorview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些输入字段的垂直线性布局.使用TextInputLayout可以得到带有标签和内置错误消息的良好流程.我的问题是当我添加和删除错误消息时.

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.

如果我使用setError(null)删除了错误消息,则消息将被删除,但是空间仍然存在.这显然是根据Google的设计(请参见 https://code.google.com/p/android/issue/detail?id = 176005 ).我非常希望删除此空间,因为它会使UI看起来非常错误.

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.

如果我执行.setErrorEnabled(false),则该视图将被删除,并且一切恢复正常.但是,如果用户更改数据并且我执行另一个setError,则不会显示错误消息(仅编辑文本行为红色).

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).

推荐答案

从支持库23.1.1版开始(可能是更早的版本),情况不再如此.您应该能够调用 TextInputLayout.setErrorEnabled(false)来隐藏错误TextView,并调用 TextInputLayout.setError(error)现在内部调用 TextInputLayout.setErrorEnabled(true)(如果错误不为null或为空).请参阅以下来自支持库的代码段:

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);
    }
    ...
}

这篇关于删除错误消息时,不会删除TextInputLayout的errorview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:34
查看更多