android - Android 中 isEmpty 时如何修复背景红色 TextInputLayout

标签 android android-edittext android-textinputlayout

我想要 setErrorTextInputLayout isEmpty 时,我写了这段代码但是当显示错误消息时,为 TextInputLayout 设置红色背景!

不想设置背景! 我想要只显示错误信息。

enter image description here

我的代码:

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/

相关文章:

android - 使用 LeanBack 支持库自定义 Android TV 界面

Android 找不到构建工具版本 X

android - 在对话框中显示键盘

android - OnEditorActionListener 不工作

android - TextInputLayout 下划线颜色在聚焦时不适应自定义颜色

android - 与 ViewPager 一起使用时扩展 TextInputLayout 时出错

android - 使用 AES 算法加密和解密

android - 如何在 Android 中使用来自 URL 的 http 身份验证信息?

android - EditText afterTextChanged 不工作 : displays null

android - 如何在 TextInputLayout 中同时显示 PasswordToggle 图标和可绘制背景?