问题描述
我第一次使用新的Android小部件TextInputLayout,这非常好,但是使用setError方法遇到一些问题
for the first time I'm using the new Android's widget TextInputLayout, it's very nice but I'm facing some problem using setError method
这是我的xml
<android.support.design.widget.TextInputLayout
android:id="@+id/userData_txtNameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColorHint="@color/light_gray"
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">
<EditText
android:id="@+id/userData_txtName"
style="@style/bold_textbox_style"
android:layout_width="match_parent"
android:layout_height="@dimen/textinut_height"
android:layout_margin="5dp"
android:hint="name"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingTop="10dp"
android:textSize="@dimen/medium_text"/>
</android.support.design.widget.TextInputLayout>
发生了什么事
我跑步时
setError("error message")
整个EditText背景和提示文本的颜色变为红色,这很好.问题是我运行
the whole EditText background and hint text color becomes red and since here it's fine. The issue is when I run
setError(null)
EditText的样式与原始样式完全不同.
the EditText's style is completely changed from the original one.
开始情况:
不专心专注
之后 setError("mandatory field")
之后 setError(null)
我做了很多研究,但找不到任何帮助,问题到底出在哪里?
I made a lot of researches but couldn't find anything helpful, what the hell should the problem be??
更新
研究setError()
方法的android源代码,我发现了这一点
Investigating in the android source code of setError()
method I found this
public void setError(@Nullable CharSequence error) {
if (!mErrorEnabled) {
if (TextUtils.isEmpty(error)) {
// If error isn't enabled, and the error is empty, just return
return;
}
// Else, we'll assume that they want to enable the error functionality
setErrorEnabled(true);
}
if (!TextUtils.isEmpty(error)) {
ViewCompat.setAlpha(mErrorView, 0f);
mErrorView.setText(error);
ViewCompat.animate(mErrorView)
.alpha(1f)
.setDuration(ANIMATION_DURATION)
.setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
@Override
public void onAnimationStart(View view) {
view.setVisibility(VISIBLE);
}
})
.start();
// Set the EditText's background tint to the error color
mErrorShown = true;
updateEditTextBackground();
updateLabelVisibility(true);
} else {
if (mErrorView.getVisibility() == VISIBLE) {
ViewCompat.animate(mErrorView)
.alpha(0f)
.setDuration(ANIMATION_DURATION)
.setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
@Override
public void onAnimationEnd(View view) {
view.setVisibility(INVISIBLE);
updateLabelVisibility(true);
}
}).start();
// Restore the 'original' tint, using colorControlNormal and colorControlActivated
mErrorShown = false;
updateEditTextBackground();
}
}
private void updateEditTextBackground() {
if (mErrorShown && mErrorView != null) {
// Set the EditText's background tint to the error color
ViewCompat.setBackgroundTintList(mEditText,
ColorStateList.valueOf(mErrorView.getCurrentTextColor()));
} else if (mCounterOverflowed && mCounterView != null) {
ViewCompat.setBackgroundTintList(mEditText,
ColorStateList.valueOf(mCounterView.getCurrentTextColor()));
} else {
final TintManager tintManager = TintManager.get(getContext());
ViewCompat.setBackgroundTintList(mEditText,
tintManager.getTintList(R.drawable.abc_edit_text_material));
}
}
并调试代码,我发现在updateEditTextBackground()
中执行的那段代码如下
and debugging the code I found that the piece of code getting executed in updateEditTextBackground()
is the following
final TintManager tintManager = TintManager.get(getContext());
ViewCompat.setBackgroundTintList(mEditText,
tintManager.getTintList(R.drawable.abc_edit_text_material));
似乎android可以任意替换EditText的背景色.我尝试使用此代码在我的可绘制文件夹中创建一个名为abc_edit_text_material.xml的文件
It seem that android is arbitrary replacing the EditText's background tint. I tryed to create a file in my drawable folder named abc_edit_text_material.xml with this code
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
android:insetTop="@dimen/abc_edit_text_inset_top_material"
android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">
<selector>
<item android:state_enabled="false" android:drawable="@color/white"/>
<item android:state_pressed="false" android:state_focused="false" android:drawable="@color/white"/>
<item android:drawable="@color/white"/>
</selector>
</inset>
但这是setError(null)
此外,我注意到只有在运行setError("error message")然后再运行setError(null)时,问题才存在
Moreover I noticed that the problem exists only when I run setError("error message") and then setError(null)
更新2 这是我用来验证输入内容的代码
UPDATE 2This is the code I use to validate my inputs
public boolean validateInputs() {
mTxtNameWrapper.setError(null);
mTxtLastNameWrapper.setError(null);
mTxtEmailWrapper.setError(null);
mTxtCountryWrapper.setError(null);
mTxtIdCardWrapper.setError(null);
mTxtFiscalCodeWrapper.setError(null);
mLblDocTypeError.setVisibility(View.GONE);
if (Strings.isNullOrEmpty(mTxtName.getText().toString())) {
mTxtNameWrapper.setError("Mandatory field");
return false;
}
if (Strings.isNullOrEmpty(mTxtLastName.getText().toString())) {
mTxtLastNameWrapper.setError("Mandatory field");
return false;
}
if (Strings.isNullOrEmpty(mTxtEmail.getText().toString())) {
mTxtEmailWrapper.setError("Mandatory field");
return false;
}
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(mTxtEmail.getText().toString()).matches()) {
mTxtEmailWrapper.setError("Invalid email format");
return false;
}
if (Strings.isNullOrEmpty(mTxtCountry.getText().toString())) {
mTxtCountryWrapper.setError("Mandatory field");
return false;
}
if (mRdgIdType.getCheckedRadioButtonId() == -1) {
mLblDocTypeError.setText("Select a document type");
mLblDocTypeError.setVisibility(View.VISIBLE);
return false;
}
if (Strings.isNullOrEmpty(mTxtIdCard.getText().toString())) {
mTxtIdCardWrapper.setError("Mandatory field");
return false;
}
if (Strings.isNullOrEmpty(mTxtFiscalCode.getText().toString())) {
mTxtFiscalCodeWrapper.setError("Mandatory field");
return false;
}
return true;
}
我快疯了!
推荐答案
我遇到了类似的问题,并找到了一个简单的解决方案.如果将TextInputLayout
中的EditText
设置为自定义背景可绘制/颜色,则会出现此问题.解决方案是将TextInputLayout
子类化,并覆盖setError()
和drawableStateChanged()
方法,然后将我们的自定义drawable/color再次设置为EditText's
背景.例如,我为我的EditText's
背景设置了一个圆角可绘制集,下面是我的子类,
I ran into similar problem and found a simple solution for it. This problem occurs if we set a custom background drawable/color to the EditText
inside the TextInputLayout
. Solution to this would be to subclass the the TextInputLayout
and override the setError()
and drawableStateChanged()
methods and set our custom drawable/color as the EditText's
background again. For Example, I had a rounded corner drawable set for my EditText's
background, below is my subclass,
public class RoundedBorderedTextInputLayout extends TextInputLayout {
private Context context;
public RoundedBorderedTextInputLayout(Context context) {
super(context);
this.context = context;
}
public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
EditText editText = getEditText();
if(editText != null) {
editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
}
}
@Override
public void setError(@Nullable final CharSequence error) {
super.setError(error);
EditText editText = getEditText();
if(editText != null) {
editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
}
}
}
然后在xml中使用您的自定义类,
And then use your custom class in the xml,
<com.example.RoundedBorderedTextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</com.example.RoundedBorderedTextInputLayout>
希望这会有所帮助.快乐的Android编码:)
Hope this helps. Happy Android coding :)
这篇关于android TextInputLayout将错误设置为null后更改EditText样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!