I am trying to validate my android Edit text box so that when the button is pressed if the Edit Text box is empty a message will appear, i am also validating the Edit text box to make sure that the value entered is a number, if the text box is empty the error message will appear saying that the value must be a number, I think it has something to do with the structure of my if statements but im not sure what. the code below is what i have tried already.
TextView Display;
EditText Input;
Display = (TextView) findViewById(R.id.tvOutput);
Input = (EditText) findViewById(R.id.eName);
try{
String UserInput = Input.getText().toString();
int number = Integer.parseInt(UserInput);
if(!UserInput.isEmpty()){
if(number == (int) number){
Display.setText(UserInput);
}
}else{
Display.setText("Please enter a value into the text box");
}
}catch(Exception e){
Display.setText("Please enter a number");
}
Aucun commentaire:
Enregistrer un commentaire