Android EditText的TextWatcher監聽回撥引數詳解
阿新 • • 發佈:2019-02-14
先貼程式碼
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
- s : 之前輸入的文字
- start : 當前輸入第一個字元的下標,從0開始
- count : 不知道為何一直為0
- after : 本次輸入的個數(但是看變數名不像啊(⊙o⊙)…)
onTextChanged
- s : 當前輸入的所有文字(介面展示的文字)
- start : 當前輸入的第一個字元的下標,從0開始
- count : 本次輸入的個數
- before : 不知道為何一直為0
afterTextChanged
- s : 當前輸入的所有文字(介面展示的文字) 與onTextChanged引數中的s值是一致的