m7m*_*idi 5 android android-layout android-edittext android-textinputlayout android-textinputedittext
我在 textInputLayout 中使用 textInputEditText 我必须为我的editText设置背景以实现我的editText的边框视图。但是当我在 textInputLayout 上调用 setError() 时,整个 editText 颜色变为红色。但我只想更改错误文本的颜色,而不是整个视图。
设置错误前:
设置错误后:
这是我的 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>
Run Code Online (Sandbox Code Playgroud)
请帮帮我,我做错了什么?
我通过扩展 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2816 次 |
| 最近记录: |