Android: Validación de texto editado con TextWatcher y .setError ()
He implementado una validación simple para un TextEdit, usando este código:
title = (EditText) findViewById(R.id.title);
title.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (title.getText().length() < 1) {
title.setError( "Title is required" );
} else {
title.setError(null);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
La función verifica, si hay algún texto insertado en un cambio de texto y todo funciona perfectamente, a menos que cuando coloque el cursor en el campo de título que ya está vacío, y presione eliminar una vez más. el mensaje de error se restablece y no se llama a textwatcher, porque no hay cambio de texto. ¿Cómo puedo mostrar el mensaje de error en este caso?