Edittext underline color when contain text (“>Loll

2020-06-29 18:57发布

I want to change the underline color of my editText when they have text inside them otherwise make a default color.

whatever the state, I want to do the following
Edittext: When they have text -> Underline color (Blue)
Edittext: when they empty -> underline color (Gray)

It works on Lollipop and below, but it's not working on Android versions above Lollipop.

I did the following:
XML

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_password_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:gravity="center_vertical"
        app:errorTextAppearance="@style/itl_error_appearance"
        app:hintTextAppearance="@style/itl_normal_appearance"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/input_email_layout"
        app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:singleLine="true"
            android:textColor="@color/colorTextDark"
            android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>

I've made a TextWatcher to do the job, but it's not working on (Android > 21 "Lollipop")


JAVA

@Override
public void afterTextChanged(Editable s) {
    String email = edtLoginEmail.getText().toString();
            if (!TextUtils.isEmpty(email)) {
                DrawableCompat.setTint(edtLoginEmail.getBackground(), ContextCompat.getColor(getActivity(), R.color.colorBlue));
        //      edtLoginEmail.getBackground().mutate().setColorFilter(getResources().getColor(R.color.colorBlue), PorterDuff.Mode.SRC_ATOP);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorBlue));
                    edtLoginEmail.setBackgroundTintList(colorStateList);
                    ViewCompat.setBackgroundTintList(edtLoginEmail, colorStateList);
                }
                edtLoginEmail.getBackground().setColorFilter(getResources().getColor(R.color.colorBlue), PorterDuff.Mode.SRC_ATOP);
            } else {
                DrawableCompat.setTint(edtLoginEmail.getBackground(), ContextCompat.getColor(getActivity(), R.color.colorGray));
        //      edtLoginEmail.getBackground().mutate().setColorFilter(getResources().getColor(R.color.colorGray), PorterDuff.Mode.SRC_ATOP);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorGray));
                    edtLoginEmail.setBackgroundTintList(colorStateList);
                    ViewCompat.setBackgroundTintList(edtLoginEmail, colorStateList);
                }
                 edtLoginEmail.getBackground().setColorFilter(getResources().getColor(R.color.colorGray), PorterDuff.Mode.SRC_ATOP);
            }
        }
    };


My main theme:

<style name="AppTheme" >
    .....
    <item name="colorControlNormal">@color/colorGray</item>
    <item name="colorControlActivated">@color/colorBlue</item>
    <item name="colorControlHighlight">@color/colorBlue</item>
    .....
</style>


Any help?

0条回答
登录 后发表回答