To illustrate further the answer given by Prithviraj, TextInputLayout
does not do the validation itself. It is just a mechanism to show the error or hint. You are responsible for setting/clearing the error. Here is how you can do that. Note that in addition to TextChangedListener, you may also need OnFocusChangeListener to set the error when user jumps to second edit text without doing any modification in the first field.
protected void onCreate(Bundle savedInstanceState) {
//.....
edtPhone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
validateEditText(s);
}
});
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
validateEditText(((EditText) v).getText());
}
}
});
}
private void validateEditText(Editable s) {
if (!TextUtils.isEmpty(s)) {
layoutEdtPhone.setError(null);
}
else{
layoutEdtPhone.setError(getString(R.string.ui_no_password_toast));
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…