下面列出了android.support.design.widget.TextInputLayout#setError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showErrorMessage(View view, int errorMessageId){
// reset error messages
((TextInputLayout)referenceActivity.findViewById(R.id.inputFoodName)).setError("");
((TextInputLayout)referenceActivity.findViewById(R.id.inputCalories)).setError("");
((TextInputLayout)referenceActivity.findViewById(R.id.inputFoodAmount)).setError("");
// if the view that this is called on is a TextInputlayout, we can show the error on the TextinputLayout
if(view instanceof TextInputLayout){
TextInputLayout til = (TextInputLayout) view;
til.setError(getString(errorMessageId));
} else {
//otherwise show a generic error message
Snackbar.make(view, errorMessageId, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
public boolean isFullNameValid(TextInputLayout fullNameTextInputLayout, String newFullName) {
boolean fullNameEntered = !TextUtils.isEmpty(newFullName.trim());
boolean valid = true;
if (fullNameEntered) {
if (newFullName.length() < FULL_NAME_MIN_LENGTH) {
valid = false;
fullNameTextInputLayout.setError(context.getString(R.string.auth_full_name_field_is_too_short));
} else if (newFullName.length() > FULL_NAME_MAX_LENGTH) {
valid = false;
fullNameTextInputLayout.setError(context.getString(R.string.auth_full_name_field_is_too_long));
}
} else {
valid = false;
fullNameTextInputLayout.setError(context.getString(R.string.profile_full_name_not_entered));
}
return valid;
}
@Override
public void afterTextChanged(Editable s) {
TextInputLayout et = weakEt.get();
if (StringUtils.validate(s.toString())) {
if (et != null) et.setErrorEnabled(false);
return;
}
if (et != null) {
et.setErrorEnabled(true);
et.setError(PalmApp.getContext().getString(R.string.illegal_email_format));
}
}
private void setError(TextView view, String text) {
TextInputLayout parent = (TextInputLayout) view.getParent();
// there is a small flashing when the error is set again
// the only way to fix that is to track if the error is
// currently shown, because for some reason TextInputLayout
// doesn't provide any getError methods.
parent.setError(text);
}
@Override
public void execute(ValidationHolder validationHolder, Matcher matcher) {
TextInputLayout textInputLayout = validationHolder.getTextInputLayout();
textInputLayout.setErrorTextAppearance(mErrorTextAppearance);
textInputLayout.setErrorEnabled(true);
textInputLayout.setError(validationHolder.getErrMsg());
}
@Override
public void halt() {
for (ValidationHolder validationHolder : mValidationHolderList) {
if (validationHolder.isSomeSortOfView()) {
validationHolder.resetCustomError();
} else {
TextInputLayout textInputLayout = validationHolder.getTextInputLayout();
textInputLayout.setErrorEnabled(false);
textInputLayout.setError(null);
}
}
}
private boolean validateInput(EditText EdTxt, TextInputLayout inputLayout) {
if (EdTxt.getText().toString().trim().isEmpty()) {
inputLayout.setError(getString(R.string.err_msg_input));
requestFocus(EdTxt);
return false;
} else {
inputLayout.setErrorEnabled(false);
}
return true;
}
public boolean isLoginDataValid(TextInputLayout emailTextInputLayout,
TextInputLayout passwordTextInputLayout, String email, String password) {
boolean isEmailEntered = !TextUtils.isEmpty(email.trim());
boolean isPasswordEntered = !TextUtils.isEmpty(password.trim());
if (isEmailEntered && isPasswordEntered) {
boolean valid = true;
if (!isEmailValid(email)) {
valid = false;
emailTextInputLayout.setError(context.getString(R.string.auth_email_field_is_incorrect));
}
if (password.length() < PASSWORD_MIN_LENGTH) {
valid = false;
passwordTextInputLayout.setError(context.getString(R.string.auth_password_field_is_too_short));
}
return valid;
} else if (!isEmailEntered && !isPasswordEntered) {
emailTextInputLayout.setError(context.getString(R.string.auth_not_all_fields_entered));
passwordTextInputLayout.setError(context.getString(R.string.auth_not_all_fields_entered));
} else {
if (!isEmailEntered) {
emailTextInputLayout.setError(context.getString(R.string.auth_email_field_not_entered));
}
if (!isPasswordEntered) {
passwordTextInputLayout.setError(context.getString(R.string.auth_password_field_not_entered));
}
}
return false;
}
public boolean isChangePasswordDataValid(TextInputLayout oldPasswordTextInputLayout,
TextInputLayout newPasswordTextInputLayout, String oldPassword, String newPassword) {
boolean isOldPasswordEntered = !TextUtils.isEmpty(oldPassword.trim());
boolean isNewPasswordEntered = !TextUtils.isEmpty(newPassword.trim());
if (isOldPasswordEntered && isNewPasswordEntered) {
if (!qbUser.getPassword().equals(oldPassword)) {
oldPasswordTextInputLayout.setError(context.getString(R.string.change_password_old_password_wrong));
} else {
return true;
}
} else if (!isOldPasswordEntered && !isNewPasswordEntered) {
oldPasswordTextInputLayout.setError(context.getString(R.string.change_password_all_fields_not_entered));
newPasswordTextInputLayout.setError(context.getString(R.string.change_password_all_fields_not_entered));
} else {
if (!isOldPasswordEntered) {
oldPasswordTextInputLayout
.setError(context.getString(R.string.change_password_old_password_not_entered));
}
if (!isNewPasswordEntered) {
newPasswordTextInputLayout
.setError(context.getString(R.string.change_password_new_password_not_entered));
}
}
return false;
}
@BindingAdapter({"app:error"})
public static void bindValidationError(TextInputLayout textInputLayout, int errorRes) {
if (errorRes != 0) {
textInputLayout.setError(textInputLayout.getResources().getString(errorRes));
} else {
textInputLayout.setError(null);
}
}
private boolean checkInput(TextInputLayout textInputLayout, int errorResId) {
String text = textInputLayout.getEditText().getText().toString();
if (TextUtils.isEmpty(text)) {
textInputLayout.setError(getResources().getString(errorResId));
textInputLayout.requestFocus();
return false;
}
return true;
}
private boolean checkInput(TextInputLayout textInputLayout, int errorResId) {
String text = textInputLayout.getEditText().getText().toString();
if (TextUtils.isEmpty(text)) {
textInputLayout.setError(getResources().getString(errorResId));
textInputLayout.requestFocus();
return false;
}
return true;
}
private boolean checkInput(TextInputLayout textInputLayout, int errorResId) {
String text = textInputLayout.getEditText().getText().toString();
if (TextUtils.isEmpty(text)) {
textInputLayout.setError(getResources().getString(errorResId));
textInputLayout.requestFocus();
return false;
}
return true;
}
public boolean isForgotPasswordDataValid(TextInputLayout emailTextInputLayout, String email) {
boolean isEmailEntered = !TextUtils.isEmpty(email.trim());
if (isEmailEntered) {
if (!isEmailValid(email)) {
emailTextInputLayout.setError(context.getString(R.string.forgot_password_email_field_is_incorrect));
} else {
return true;
}
} else {
emailTextInputLayout.setError(context.getString(R.string.forgot_password_email_field_not_entered));
}
return false;
}
private void setError(TextInputEditText editText, @StringRes int errorRes) {
TextInputLayout layout = (TextInputLayout) editText.getParent();
layout.setError(getString(errorRes));
}
private void removeError(TextInputEditText editText) {
TextInputLayout layout = (TextInputLayout) editText.getParent();
layout.setError(null);
}
/**
* Facilitates binding error text to a TextInputLayout.
*/
@BindingAdapter("errorText")
public static void setErrorText(TextInputLayout layout, String errorText) {
layout.setError(errorText);
}
private void setEditTextError(TextInputLayout layout, int msgId) {
layout.setErrorEnabled(true);
layout.setError(getString(msgId));
}
private void setError(TextInputEditText editText, @StringRes int errorRes) {
TextInputLayout layout = (TextInputLayout) editText.getParent().getParent();
layout.setError(getString(errorRes));
}
private void setEditTextError(TextInputLayout layout, int msgId) {
layout.setErrorEnabled(true);
layout.setError(getString(msgId));
}