1. 程式人生 > >Android EditText的TextWatcher監聽回撥引數詳解

Android 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值是一致的