我想有一个EditText的背景作为一个“正常”的EditText但错误处理的TextInputEditText(错误信息出现在底部,而不是“!”可绘制出现)。不要更改错误的TextInputLayout背景
我得到了这样的事情:
<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中。
所以这是我的EditText看起来像:
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();
}
}
该更新的研究背景颜色
我们可以以下方法中看到它在TextInputLayout的代码这里:
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
}
因为这种方法是私人的我不能重写它,因为我仍然希望我的错误TextV视图的颜色是红色的我目前看不到任何解决方案。任何想法?
一个解决方案可能会重置背景颜色为setError
本来被调用后的默认值,但他们的任何回调,如onError
方法将被解雇,一旦错误设置为TextView/EditText?
很好的解决方案。又一次,我们需要使用黑客来克服Android的糟糕设计。 – Storix
梦幻般的解决方案.... –
@覆盖! – Killer