在我第一次使用新的Android的小部件TextInputLayout,這是很不錯,但我使用SETERROR方法安卓TextInputLayout改變的EditText樣式設置錯誤後爲空
這是我的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背景和提示文字顏色變成紅色,因爲這裏沒問題。問題是我運行時
setError(null)
EditText的樣式完全改變了原來的樣式。
啓動狀態:
後setError("mandatory field")
後setError(null)
我做了很多的研究,但找不到任何有用的,應該問題是什麼?地獄
UPDATE
在setError()
方法的Android源代碼調查我發現這個
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()
獲取執行代碼以下
final TintManager tintManager = TintManager.get(getContext());
ViewCompat.setBackgroundTintList(mEditText,
tintManager.getTintList(R.drawable.abc_edit_text_material));
它看起來Android是任意replaci ng EditText的背景色調。我嘗試在名爲abc_edit_text_material的可繪製文件夾中創建一個文件。XML與此代碼
<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(「錯誤信息」),然後SETERROR(空)
UPDATE 2 這是我使用來驗證我的輸入的代碼
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;
}
我要瘋了!
setError(null)=> clearError()? –
你確定這種方法存在嗎? – Apperside
setErrorEnabled(false)? –