FAQ_10_EditText與parseInt方法

來源:互聯網
上載者:User

需求:從android的EditText中擷取輸入的數字,作為連接埠號碼。

基本思路 ,見下面代碼示意:

[java]
view plaincopyprint?
  1. String str = editor.getText().toString().trim();  
  2. int port = Integer.parseInt(str);  

如果使用者輸入的字元包括非數字如mark123,那麼,會報異常:[html]
view plaincopyprint?

  1. 08-15 07:13:48.294: ERROR/AndroidRuntime(12281): java.lang.NumberFormatException: unable to parse 'dd' as integer  

parseInt()源碼:

[java]
view plaincopyprint?
  1. /** 
  2.     * Parses the specified string as a signed decimal integer value. The ASCII 
  3.     * character \u002d ('-') is recognized as the minus sign. 
  4.     * 
  5.     * @param string 
  6.     *            the string representation of an integer value. 
  7.     * @return the primitive integer value represented by {@code string}. 
  8.     * @throws NumberFormatException 
  9.     *             if {@code string} is {@code null}, has a length of zero or 
  10.     *             can not be parsed as an integer value. 
  11.     */  
  12.    public static int parseInt(String string) throws NumberFormatException {  
  13.        return parseInt(string, 10);  
  14.    }  

可以看出,參數字串為null、或者長度為0以及非數位字元時,報NumberFormatException異常。

那麼,如何解決這個問題?

簡單辦法,就是捕捉異常資訊:

[java]
view plaincopyprint?
  1. String str = editor.getText().toString().trim();  
  2. try {  
  3.     int port = Integer.parseInt(str);  
  4. } catch(NumberFormatException nfe) {  
  5.     // to do  
  6. }  

還有一個辦法,利用EditText的xml屬性(android:digits)。

[html]
view plaincopyprint?
  1. <EditText  
  2.     android:id="@+id/edit"    
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"  
  5.     android:inputType="number"   
  6.     android:digits="0123456789"/>  

這樣以來,在該EditText中只可以輸入數字,其它任何字元都不會被接受。如斷行符號、空格等。

類似的,只接受大小寫字母:

[html]
view plaincopyprint?
  1. android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"只可以輸入52個大小寫字母  

或者只接受數字與字母:

[html]
view plaincopyprint?
  1. android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"  

關於 parseInt() 用法,下面再示一例:

[java]
view plaincopyprint?
  1. // 將16進位轉換為10進位  
  2.         int decimalNum = Integer.parseInt("101a", 16);  
  3.         System.out.println("decimalNum= " + decimalNum);  
  4.           
  5.         // 將10進位轉換為16進位  
  6.         String str = Integer.toHexString(100);  
  7.         System.out.println("str= " + str);  
  8.         int hexNum = Integer.parseInt(str);  
  9.         System.out.println("hexNum= " + hexNum);  
  10.           
  11.         // Integer類還有toBinaryString()、toOctalString()將int轉換為2進位、8進位 

轉自:http://blog.csdn.net/androidbluetooth/article/details/6689726

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.