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

你可能感兴趣的:(Android移动开发)