Created
January 29, 2019 05:06
-
-
Save parzibyte/dd6d66f1a542ad27bdd9c90f1f5c249c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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