我想驗證我的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