我想验证我的android编辑文本框,以便当按钮被按下时,如果编辑文本框为空,则会出现一条消息,我也正在验证编辑文本框以确保输入的值是一个数字,如果文本框为空,则会显示错误消息,说明该值必须是数字,我认为这与我的if语句的结构有关,但我不确定什么。下面的代码是我已经尝试过的。如果语句没有按照正确的顺序排除语句
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");
}
'如果(编号==(INT)号)'而'数字'已经是一个int? – timrau 2015-02-08 16:45:13
即时解析通过用户输入存储在一个名为UserInput – Joshua 2015-02-08 16:47:20
的字符串中'if(number ==(int)number)'是一个无用语句,它将始终为真。这就像比较'x == x'。 – initramfs 2015-02-08 16:50:00