在 Android 9.0 中将 TextInputlayout 上的错误设置为 false 不删除额外的 space

Setting error enabled to false on TextInputlayout not removing extra space in Android 9.0

我正在使用基本 TextInputLayout。当 EditText 为空时,我正在设置错误。当用户输入一些文本时,我正在删除这样的错误文本。

 tilPassword.isErrorEnabled = false
 tilPassword.error = null

这很好用。调用 tilPassword.isErrorEnabled = false 的原因是它帮助我删除 TextInputLayout 在显示错误文本时添加到布局中的额外 space。当用户输入一些文本时,我将删除错误以及额外的 space(由 TextInputLayout 添加)。

正在删除错误消息,但在 Android 9.0 中未删除额外的 space。如何解决这个问题?

https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout.html#setErrorEnabled(boolean)

文档解释:

Enabling this functionality before setting an error message via setError(CharSequence), will mean that this layout will not change size when an error is displayed.

所以它会像这样工作。

显示错误:

tilPassword.setErrorEnabled(true)
tilPassword.setError("Wrong password")

隐藏错误:

tilPassword.setError("")
tilPassword.setErrorEnabled(false)

确保您使用的是 AndroidX 版本的 TextInputLayout:

https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout.html

由于以下平台版本已弃用:

https://developer.android.com/reference/android/support/design/widget/TextInputLayout

参考。有人解释来源: