2015-02-08 37 views
-2

我想驗證我的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"); 
} 
+0

'如果(編號==(INT)號)'而'數字'已經是一個int? – timrau 2015-02-08 16:45:13

+0

即時解析通過用戶輸入存儲在一個名爲UserInput – Joshua 2015-02-08 16:47:20

+4

的字符串中'if(number ==(int)number)'是一個無用語句,它將始終爲真。這就像比較'x == x'。 – initramfs 2015-02-08 16:50:00

回答

1

你的問題來自於Integer.parseInt()會拋出NumberFormatException爲空字符串,在if語句運行之前將控制流發送到catch塊。

將解析前的空檢查重新排序爲要修復的整數。

String UserInput = Input.getText().toString(); 

if(!UserInput.isEmpty()){ 
    try{ 
     int number = Integer.parseInt(UserInput); 

     Display.setText(UserInput); 
    }catch(NumberFormatException e){ 
     Display.setText("Please enter a number"); 
    } 
}else{ 
    Display.setText("Please enter a value into the text box"); 
} 

對於正確的Android的方式來過濾數值只考慮您的佈局XML或或通過添加android:inputType=number歡迎使用屬性到您的EditText元素:

EditText.setInputType(InputType.TYPE_CLASS_NUMBER); 

直接在java中。

+0

感謝您的幫助 – Joshua 2015-02-08 17:06:40

0

您需要驗證輸入的用戶是否已經輸入的數字,而不是字母數字值,你可以做如下:

String userInput = null; 
do { 
    Display.setText("Please enter a number"); 
    userInput = Input.getText().toString(); 
} while (!userInput.isEmpty() && !userInput.matches("[0-9]+")); 
Display.setText(userInput); 

上面做while循環,以確保用戶輸入數據及其有效輸入。如果不是這樣,那麼繼續獲得用戶輸入。

最後一次,它的有效數字,只顯示輸入數字。

0

如果文本框爲空,將顯示錯誤消息說,值必須是數字

的問題是在這個片段:

try{ 
    String UserInput = Input.getText().toString(); 
    int number = Integer.parseInt(UserInput); 

    if(!UserInput.isEmpty()){ 

你試圖將輸入轉換爲整數,然後檢查是否有可用的值。這意味着你可以嘗試轉換一個空字符串:""。這是行不通的,如果發生這種情況,Integer#parseInt將會拋出NumberFormatException。這將導致你到catch區塊,它會打印「請輸入一個數字」

爲了解決這個問題,只是檢查是否UserInput是空的,然後再嘗試將輸入轉換成整數:

String userInput = input.getText().toString(); 
if (!userInput.isEmpty()) { 
    try{ 
     Integer.parseInt(userInput); // no need to store that result in a variable 
     display.setText(userInput); 
    } catch (Exception e) { 
     display.setText("Please enter a number"); 
    } 
} else { 
    display.setText("Please enter a value into the text box"); 
} 

另外,請閱讀Java Naming Conventions,因爲每一個變量應該以小寫字母開頭。我已經更改了我的代碼示例中的變量名稱。

+0

感謝您的幫助 – Joshua 2015-02-08 17:06:13

0

這2種輔助方法可以幫助你:

  1. 檢查的EditText有文字。如果EditText上不具有文本設置錯誤消息

    public static boolean hasText(Context context, EditText editText) 
    { 
    boolean validated = true; 
    String text = editText.getText().toString().trim(); 
    if (text.length() == 0) 
    { 
        editText.setText(text); 
        validated = false; 
        Drawable d = context.getResources().getDrawable(R.drawable.alert_small); 
        d.setBounds(0, 0, 
          d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
        editText.setError(context.getString(R.string.required_field), d); 
    } 
    else 
    { 
        editText.setError(null); 
    } 
    return validated; 
    } 
    
  2. 檢查文本數

    public static boolean isNumber(String str) 
    { 
    try 
    { 
        int d = Integer.parseInt(str); 
    } 
    catch (NumberFormatException nfe) 
    { 
        return false; 
    } 
    return true; 
    

    }