我想要 setError
当 TextInputLayout
isEmpty
时,我写了这段代码但是当显示错误消息时,为 TextInputLayout
设置红色背景!
我不想设置背景! 我想要只显示错误信息。
我的代码:
if (TextUtils.isEmpty(userName)) {
register_UserName_layout.setError("Insert Username");
}
XML 代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/register_userUsernameTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/register_headerLayout"
android:layout_margin="10dp"
android:textColorHint="#c5c5c5">
<EditText
android:id="@+id/register_userUserNameText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/selector_bg_edit"
android:hint="نام کاربری"
android:paddingBottom="2dp"
android:textColor="@color/colorAccent"
android:textCursorDrawable="@drawable/bg_input_cursor"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
我该如何解决这个问题?谢谢大家<3
最佳答案
我已经找到解决这个问题的方法。您只需要创建一个自定义 EditText 并覆盖它的 getBackground() 方法以返回一个新的可绘制对象。这样 TextInputLayout 将无法在 EditText 的背景上设置颜色过滤器,因为您不返回 EditText 的背景,而是返回另一个可绘制对象。见下文:
@Override
public Drawable getBackground() {
return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}
并在 TextInputLayout 中使用自定义的 EditText:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_edit_text_bg" />
</android.support.design.widget.TextInputLayout>
关于android - Android 中 isEmpty 时如何修复背景红色 TextInputLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188609/