【发布时间】:2016-12-05 13:21:06
【问题描述】:
我想要一个背景为“正常”的 EditText,但带有 TextInputEditText 的错误处理(错误消息出现在底部,而不是“!”drawable 出现)。
我得到了这样的东西:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setError="@{viewModel.error}">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/simple_edit_text_background"
android:ellipsize="end"
android:inputType="textMultiLine|textNoSuggestions"
android:text="@={viewModel.value}"
style="@style/MyEditTextStyle" />
</android.support.design.widget.TextInputLayout>
但似乎当我在 TextInputLayout 上设置错误时,它会将背景可绘制对象(在普通 TextInputEditText 中为下划线)更改为错误 TextView 的颜色。
我们可以在TextInputLayout的代码中看到如下方法:
private void updateEditTextBackground() {
if (mEditText == null) {
return;
}
Drawable editTextBackground = mEditText.getBackground();
if (editTextBackground == null) {
return;
}
ensureBackgroundDrawableStateWorkaround();
if (android.support.v7.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
editTextBackground = editTextBackground.mutate();
}
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else if (mCounterOverflowed && mCounterView != null) {
// Set a color filter of the counter color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mCounterView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else {
// Else reset the color filter and refresh the drawable state so that the
// normal tint is used
DrawableCompat.clearColorFilter(editTextBackground);
mEditText.refreshDrawableState();
}
}
更新背景颜色的代码块在这里:
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
}
因为这个方法是私有的,所以我不能覆盖它,因为我仍然希望我的错误 TextView 的颜色为红色,所以到目前为止我看不到任何解决方案。有什么想法吗?
一种解决方案可能会在 setError 被调用之后将背景颜色重置为其默认值,但是他们的任何回调都使用像 onError 这样的方法,一旦错误设置为 TextView/EditText 就会被触发?
【问题讨论】:
标签: android android-textinputlayout