Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 29, 2019 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/dd6d66f1a542ad27bdd9c90f1f5c249c to your computer and use it in GitHub Desktop.
Save parzibyte/dd6d66f1a542ad27bdd9c90f1f5c249c to your computer and use it in GitHub Desktop.
// Agregar listener al botón
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Quitamos los errores
etNumero.setError(null);
String posibleNumero = etNumero.getText().toString();
// Vemos si está vacío...
// Notación yoda: https://parzibyte.me/blog/2018/09/24/notacion-yoda-en-programacion/
if ("".equals(posibleNumero)) {
// Primer error
etNumero.setError("Introduce un número");
// Le damos focus
etNumero.requestFocus();
// Y terminamos la ejecución
return;
}
// En caso de que hayan puesto algo, convertimos a entero
int numero = Integer.parseInt(posibleNumero);
// Comparar si está en el rango
if (numero >= 5 && numero <= 60) {
// La validación termina y hacemos lo que vayamos a hacer
Toast.makeText(MainActivity.this, "Todo correcto", Toast.LENGTH_SHORT).show();
} else {
// Si no, entonces indicamos el error y damos focus
etNumero.setError("Número fuera de rango");
etNumero.requestFocus();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment