当我的editText中包含文本时,我想更改其下划线颜色,否则将使用默认颜色。
无论状态如何,我都想执行以下操作
编辑文字:当他们有文字时->下划线颜色(蓝色)
Edittext:当它们为空时->下划线颜色(灰色)
它适用于Lollipop及以下版本,但不适用于Lollipop以上的Android版本。
我做了以下事情:
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>
我已经做了一个TextWatcher来完成这项工作,但是它不能正常运行(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);
}
}
};
我的主要主题:
<style name="AppTheme" >
.....
<item name="colorControlNormal">@color/colorGray</item>
<item name="colorControlActivated">@color/colorBlue</item>
<item name="colorControlHighlight">@color/colorBlue</item>
.....
</style>
有帮助吗?