3
When trying to use the method EditText.setError()
the error message does not appear. The code below is used to validate the required fields:
public class LoginActivity extends Activity {
private EditText usuario;
private EditText senha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usuario = (EditText) findViewById(R.id.usuario);
senha = (EditText) findViewById(R.id.senha);
}
private boolean loginValido() {
EditText campoComFoco = null;
boolean isValid = true;
if (usuario.getText().toString().length() == 0) {
campoComFoco = usuario;
usuario.setError("Usuário obrigatório");
isValid = false;
}
if (senha.getText().toString().length() == 0) {
if (campoComFoco == null) {
campoComFoco = senha;
}
senha.setError("Senha obrigatória");
isValid = false;
}
if (campoComFoco != null) {
campoComFoco.requestFocus();
}
return isValid;
}
}
Dude, without the code you made, it gets a little tricky telling you what’s missing.. Edit your question and add the relevant part of the code.
– emanuelsn
A question. Are you trying to put the error in a
TextView
or in aEditText
?– rsicarelli
Sorry, @sicachester, Edittext.
– Geison Santos