我在 textInputLayout 中使用 textInputEditText
我必须为我的 editText 设置背景才能为我的 editText 实现边框视图。
但是当我在 textInputLayout 上调用 setError() 时,整个 editText 颜色变为红色。
但我只想更改错误文本的颜色,而不是整个视图。
设置错误之前:
截屏 https://i.stack.imgur.com/GqNSG.jpg
设置错误后:
截屏 https://i.stack.imgur.com/w1Jxh.jpg
这是我的 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>
请帮助我,我做错了什么?
我通过扩展 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();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)