当错误设置为 null 时 TextInputLayout 留下空白

Shu*_*udy 5 user-interface android kotlin

我的用户界面有一点问题。

我用来com.google.android.material.textfield.TextInputLayout进行登录,并以简单的方式设置错误。

情况:

我有空TextInputLayout,没有错误可显示。

在此输入图像描述

然后当用户开始写入时,我设置TextInputLayout.error = "Error Messge". 因为这里一切都是正确的。

在此输入图像描述

但是当用户清理 时TextInputLayout,我清理了将其设置为 null 的错误:TextInputLayout.error = null

在此输入图像描述

这就是问题所在。清除错误后,显示错误的空间不会消失。它留下了我不需要的空白。

有什么办法可以去除吗?我看不到任何方法来“清除”错误并删除此空间。

PS:算法我尝试设置为“空白”而不是 null --> TextInputLayout.error = "",但不起作用。

小智 6

您需要禁用删除空格时的错误。然后,如果您想将来显示错误消息,则需要再次启用它。

例子:

if (exampleTextInput.editText?.text?.isEmpty()!!) {
    this.exampleTextInput.error = null
    exampleTextInput.isErrorEnabled = false
} else {
    exampleTextInput.isErrorEnabled = true
    this.exampleTextInput.error = "error message"
}
Run Code Online (Sandbox Code Playgroud)