我试图使用MaterialComponents TextInputLayout实现一个表单。根据 文件它应该有一个错误信息的空间。我已经在我的布局xml中启用了错误信息,它也显示在Android Studio布局预览中,但在应用程序中不起作用。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/name_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:hint="@string/hint_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryText"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
预览。
到底发生了什么?
我通过在输入有效时禁用错误信息后再启用错误信息来解决这个问题。
else { // Condition finally met when error is valid
signinEmailLayout.setErrorEnabled(false);
signinEmailLayout.setErrorEnabled(true);
}
正如@MRamzan在评论中建议的那样,这似乎是默认行为。总之,在我的activity类中,我将errorEnabled设置为false,这导致布局在输入有效时删除了为错误信息指定的空间。只要输入再次无效,错误信息就会被添加,因为我再次启用了错误信息,因此出现了不稳定的UX。