EditText控件颜色

时间:2015-12-08 17:45:29

标签: android material-design

我尝试在用户输入错误的凭据(例如googles material design TextField material design后)更改EditText控件(行)的颜色,尽管似乎没有任何工作正常我最近的情况是使用< / p>

passwordEditText.getBackground().setColorFilter(getResources().getColor(R.color.error_color), PorterDuff.Mode.SRC_ATOP);
passwordTextInput.setError(errorHelper.getErrorMessage(e));
passwordTextInput.setErrorEnabled(true);`

虽然这可能有效,但我仍然有旧的颜色在后台 enter image description here

任何想法?

使用的样式

<style name="LoginEditTextTheme" parent="@style/ThemeOverlay.AppCompat">
    <item name="colorControlNormal">@color/login_edit_text_line</item> //black
    <item name="colorControlActivated">@android:color/widget_edittext_dark</item> //black
    <item name="colorControlHighlight">@android:color/black</item> //black
    <item name="colorAccent">@color/login_hint</item> //light grey
</style>

XML

<android.support.design.widget.TextInputLayout
            android:id="@+id/password_textlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/LoginEditTextTheme">

            <EditText
                android:id="@+id/password_edittext"
                style="@style/LoginEditTextStyle" //no theme here 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_password_hint"
                android:inputType="textPassword"
                android:nextFocusDown="@+id/login_button" />
</android.support.design.widget.TextInputLayout>

1 个答案:

答案 0 :(得分:1)

试试这个方法:

public static void tintWidget(View view, int color) {
    Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
    DrawableCompat.setTint(wrappedDrawable, getResources().getColor(color));
    view.setBackgroundDrawable(wrappedDrawable);
}

您可以将所需的颜色和EditText引用传递给此帮助器方法,它将为Editaxt的底线着色为ya。这依赖于 appcompat-v7:至少22.1.0+库