修改Android源碼計算機——輸入框最大輸入數提示

來源:互聯網
上載者:User

 Android源碼計算機:當輸入到一定數,加減乘除一些數後,容易報錯。

2.       想在計算機的數字輸入框中,加入限制最大輸入數並提示。

用如下方法本來可以很輕意地解決此問題 ,但由於布局運用到了:CalculatorDisplay,其extends了ViewSwitcher,在lengthFilter 需要用到參數:EditText,即輸入框。由於對android源碼計算機不熟,一時不知如何擷取計算機輸入框,後來發現通過CalculatorDisplay的getChildAt(1)可以取得。在onCreate調用如下lengthFilter方法,如:lengthFilter(this,(EditText)mDisplay.getChildAt(1),50,getResources().getString(R.string.most_char));  可以解決

  1. public static void lengthFilter(final Context context, final EditText editText, final int max_length, final String err_msg) {  
  2.   
  3.         InputFilter[] filters = new InputFilter[1];  
  4.   
  5.         filters[0] = new InputFilter.LengthFilter(max_length) {  
  6.   
  7.             @Override  
  8.             public CharSequence filter(CharSequence source, int start, int end,  
  9.                     Spanned dest, int dstart, int dend) {  
  10.                 // TODO Auto-generated method stub   
  11.                   
  12.                 int destLen = getCharacterNum(dest.toString()); //擷取字元個數(一個中文算2個字元)   
  13.                 int sourceLen =  getCharacterNum(source.toString());   
  14.                 if (destLen + sourceLen > max_length) {  
  15.                 Toast.makeText(context, err_msg,Toast.LENGTH_SHORT).show();  
  16.                 return "";  
  17.                 }  
  18.                 return source;  
  19.                   
  20.             }  
  21.   
  22.         };  
  23.           
  24.         editText.setFilters(filters);  
  25.   
  26.         }  
  27.       
  28.         /** 
  29.          * @description 擷取一段字串的字元個數(包含中英文,一個中文算2個字元) 
  30.          * @param content 
  31.          * @return 
  32.          */  
  33.         public static int getCharacterNum(final String content) {  
  34.         if (null == content || "".equals(content)) {  
  35.         return 0;  
  36.         }else {  
  37.         return (content.length() + getChineseNum(content));  
  38.         }  
  39.         }  
  40.       
  41.       
  42.       
  43.       
  44.         /** 
  45.         * @description 返回字串裡中文字或者全形字元的個數 
  46.         * @param s  
  47.         * @return 
  48.         */  
  49.         public static int getChineseNum(String s) {  
  50.       
  51.         int num = 0;  
  52.         char[] myChar = s.toCharArray();  
  53.         for (int i = 0; i < myChar.length; i++) {  
  54.         if ((char)(byte)myChar[i] != myChar[i]) {  
  55.         num++;  
  56.         }  
  57.         }  
  58.         return num;  
  59.         }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.