1. 程式人生 > >Android EditText實現只能輸入幾位整數幾位小數

Android EditText實現只能輸入幾位整數幾位小數

EditText的TextWatcher監聽幾個回撥方法的解釋

        mEditText = (EditText) findViewById(R.id.edittext);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("CustomActivity"
,"beforeTextChanged"); Log.i("beforeTextChanged","s = " + s); Log.i("beforeTextChanged","start = " + start); Log.i("beforeTextChanged","count = " + count); Log.i("beforeTextChanged","after = " + after); } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { Log.i("CustomActivity","onTextChanged"); Log.i("onTextChanged","s = " + s); Log.i("onTextChanged","start = " + start); Log.i("onTextChanged","count = "
+ count); Log.i("onTextChanged","before = " + before); } @Override public void afterTextChanged(Editable s) { Log.i("CustomActivity","afterTextChanged"); Log.i("afterTextChanged","s = " + s); } });

當輸入第一個字元”0”的時候,log如下

06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: beforeTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: s = 
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: start = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: count = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: after = 1
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: onTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: s = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: start = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: count = 1
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: before = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: afterTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/afterTextChanged: s = 0

當連續輸入兩個字元的時候(用搜狗輸入法打”你好”,PS:記得連拼),log如下

06-28 20:51:50.415 28820-28820/com.alfred.chowder I/CustomActivity: beforeTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: s = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: start = 1
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: count = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: after = 2
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/CustomActivity: onTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: s = 0你好
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: start = 1
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: count = 2
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: before = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/CustomActivity: afterTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/afterTextChanged: s = 0你好

分析如下

beforeTextChanged

  1. s : 之前輸入的文字
  2. start : 當前輸入第一個字元的下標,從0開始
  3. count : 不知道為何一直為0
  4. after : 本次輸入的個數(但是看變數名不像啊(⊙o⊙)…)

onTextChanged

  1. s : 當前輸入的所有文字(介面展示的文字)
  2. start : 當前輸入的第一個字元的下標,從0開始
  3. count : 本次輸入的個數
  4. before : 不知道為何一直為0

afterTextChanged

  1. s : 當前輸入的所有文字(介面展示的文字) 與onTextChanged引數中的s值是一致的

程式碼實現 EditText只能輸入三位整數兩位小數(幾位整數幾位小數可以隨意設定)

     * 整數位數
     */
    private static final int INTEGER_COUNT = 3;
    /**
     * 小數位數
     */
    private static final int DECIMAL_COUNT = 2;

    mEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("InputLimitActivity", "beforeTextChanged");
                Log.i("beforeTextChanged", "s = " + s);
                Log.i("beforeTextChanged", "start = " + start);
                Log.i("beforeTextChanged", "count = " + count);
                Log.i("beforeTextChanged", "after = " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("InputLimitActivity", "onTextChanged");
                Log.i("onTextChanged", "s = " + s);
                Log.i("onTextChanged", "start = " + start);
                Log.i("onTextChanged", "count = " + count);
                Log.i("onTextChanged", "before = " + before);
                if (start == 0 && s.toString().equals(".") && count == 1) {
                    //輸入的第一個字元為"."
                    mEditText.setText("");
                } else if (s.length() >= INTEGER_COUNT + 1 && count != 0) {
                    //當整數位數輸入到達被要求的上限,並且當前在輸入字元,而不是減少字元
                    if (s.toString().contains(".")) {
                        //當前輸入的有"."字元
                        String[] text = s.toString().split("\\.");
                        if (text.length >= 2 && text[1].length() > DECIMAL_COUNT) {
                            //小數位數超數
//                          s.delete(text[0].length() + text[1].length() + 1, s.toString().length());
//                          mEditText.setText(s);
                            mEditText.setText(s.subSequence(0, s.toString().length() - 1));
                            mEditText.setSelection(s.toString().length() - 1);
                        }
                    } else {
//                      s.delete(s.toString().length() - 1, s.toString().length());
//                      mEditText.setText(s);
                        mEditText.setText(s.subSequence(0, s.toString().length() - 1));
                        mEditText.setSelection(s.toString().length() - 1);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("InputLimitActivity", "afterTextChanged");
                Log.i("afterTextChanged", "s = " + s);


            }
        });

完美實現

實現的效果

只能輸入一個小數,並且第一位不能輸入為小數
只能輸入三位整數,兩位小數(這個可以自己配置)
當輸入第四位整數時,不能顯示在螢幕上,也就是輸入不進去

但是這裡有個問題

在afterTextChanged方法裡面,我註釋掉了四行程式碼,這是一種實現方式(但是超出範圍的字元),但是這種方式丟擲了StackOverFlowError,我不知道是為什麼,有懂得請留言,在下先謝過[抱拳]!只能換一種實現方式來實現(String.subSequence()),最後實現的效果是一樣的