【问题标题】:android textInputEditText with custom background is not working ok具有自定义背景的android textInputEditText无法正常工作
【发布时间】:2017-11-28 05:20:35
【问题描述】:

我在 textInputLayout 中使用 textInputEditText 我必须为我的editText 设置背景,以实现我的editText 的边框视图。 但是当我在我的 textInputLayout 上调用 setError() 时,整个 editText 颜色变为红色。 但我只想更改错误文本的颜色,而不是整个视图。

设置错误前:

screen shot

设置错误后:

screen shot

这是我的 xml 代码:

<android.support.design.widget.TextInputLayout
            android:layout_alignParentTop="true"
            android:id="@+id/ex_pass_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="false"
            android:gravity="right">
            <android.support.design.widget.TextInputEditText
                android:id="@+id/ex_pass_et"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:hint="رمز عبور فعلی"
                android:inputType="textPassword"
                android:textColor="#000"
                android:textSize="15sp"
                android:gravity="right|center"
                android:background="@drawable/edittext_bg"
                android:padding="8dp"
                />
        </android.support.design.widget.TextInputLayout>

请帮帮我,我做错了什么?

【问题讨论】:

  • 你希望发生什么?
  • @Code-Apprentice 我只想更改错误文本,而不是整个视图
  • 我不清楚我在您的屏幕截图中看到的内容。这两个不同的文本字段是否在同一个屏幕中?或者你想展示“之前”和“之后”?红色文本字段上方和下方的文本是什么?我看到上面的文本与白色文本字段中的文本相同。这是用户输入的数据吗?红色文本字段下方的红色文本呢?这是错误信息吗?
  • @Code-Apprentice 这些是“之前”和“之后”的屏幕截图。上面的文字是提示,下面的文字(红色)是错误文字
  • 如果你发布两张单独的照片会更清楚。

标签: android android-layout android-edittext android-textinputlayout android-textinputedittext


【解决方案1】:

我通过扩展 TextInputLayout 并覆盖一些方法来解决这个问题

public class CustomTextInputLayout extends TextInputLayout {

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    public void setError(@Nullable CharSequence error) {
        super.setError(error);
        try {
            EditText et = getEditText();
            Drawable editTextBackground = et.getBackground();
            editTextBackground.clearColorFilter();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        try {
            EditText et = getEditText();
            Drawable editTextBackground = et.getBackground();
            editTextBackground.clearColorFilter();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多