EditText TextView 输入内容变化监听 TextWatcher 参数详解(最全面详细的讲解)

一,EditText 继承 TextView,因此二者在处理 输入内容变化监听的方式上是一致的

public class EditText extends TextView {
    public EditText(Context context) {
        this(context, null);
    }

二, 输入内容变化监听,有两种实现方式

(1) TextView 的 onTextChanged() 方法

    /**
     *  textview 内容发生变化后,回调该方法,需要子类实现
     * @param text 变化后的内容
     * @param start 从哪个位置开始发生变化的
     * @param lengthBefore 删除的字符个数(增加内容该值为0)
     * @param lengthAfter 增加的字符个数(删除内容该值为0)
     */
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        // intentionally empty, template pattern method can be overridden by subclasses
    }
}

demo演示:

@SuppressLint("AppCompatCustomView")
public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        Log.d("CustomEditText", "CharSequence text: " + text + " ||int start " + start + " ||int lengthBefore: " + lengthBefore + " ||int lengthAfter: " + lengthAfter);
    }
}

复制字符串:abcd 然后再输入 2

09-03 19:29:44.005 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 0 int lengthBefore: 0 int lengthAfter: 4
09-03 19:30:00.883 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd2 int start 4 int lengthBefore: 0 int lengthAfter: 1

删除 2

09-03 19:29:44.005 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 0 int lengthBefore: 0 int lengthAfter: 4
09-03 19:30:00.883 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd2 int start 4 int lengthBefore: 0 int lengthAfter: 1
09-03 19:31:01.029 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 4 int lengthBefore: 1 int lengthAfter: 0

demo和我们上面的结论一致

二,使用接口 public interface TextWatcher 监听.

public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = findViewById(R.id.edittext);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d("TAG", "TAG:== CharSequence  beforeTextChanged:     s= " + s + "    ||int start " +   start + "   ||int count: " + count + "   ||int after: " + after + "\n");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("TAG", "TAG:== CharSequence  onTextChanged:         s= " + s + " ||int start " +   start + " ||int before: " + before + "  ||int count: " +  count + "\n");
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d("TAG", "TAG:== CharSequence afterTextChanged        s= " + s.toString());
            }
        });
    }
}

测试demo:
复制字符串:abcd 再输入 3

09-03 19:57:18.849 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  beforeTextChanged:     s=     ||int start 0   ||int count: 0   ||int after: 4
09-03 19:57:18.853 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  onTextChanged:         s= abcd ||int start 0 ||int before: 0  ||int count: 4
09-03 19:57:18.868 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged        s= abcd
09-03 19:57:32.759 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  beforeTextChanged:     s= abcd    ||int start 4   ||int count: 0   ||int after: 1
09-03 19:57:32.760 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  onTextChanged:         s= abcd3 ||int start 4 ||int before: 0  ||int count: 1
09-03 19:57:32.776 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged        s= abcd3

然后删除 3

09-03 19:57:18.849 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  beforeTextChanged:     s=     ||int start 0   ||int count: 0   ||int after: 4
09-03 19:57:18.853 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  onTextChanged:         s= abcd ||int start 0 ||int before: 0  ||int count: 4
09-03 19:57:18.868 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged        s= abcd
09-03 19:57:32.759 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  beforeTextChanged:     s= abcd    ||int start 4   ||int count: 0   ||int after: 1
09-03 19:57:32.760 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  onTextChanged:         s= abcd3 ||int start 4 ||int before: 0  ||int count: 1
09-03 19:57:32.776 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged        s= abcd3
09-03 19:59:53.353 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  beforeTextChanged:     s= abcd3    ||int start 4   ||int count: 1   ||int after: 0
09-03 19:59:53.354 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence  onTextChanged:         s= abcd ||int start 4 ||int before: 1  ||int count: 0
09-03 19:59:53.356 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged        s= abcd

TextWatcher 回调参数详解


TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // s  未改变之前的内容
            // start 内容被改变的的开始位置
            // count 原始文字被删除的个数
            // after 新添加的文字个数
            //========================= start 和 count 结合 获取被删除的文字 ===================
           // String deleteText = s.toString().substring(start + 1 - count, start + 1);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // s  改变之后的内容
            // start 内容被改变的的开始位置
            // before 原始文字被删除的个数
            // count 新添加的文字个数
            //========================= start 和 count 结合 获取新添加的文字 ===================
            String addText = s.toString().substring(start, start + count);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // s  改变之后的最终内容
        }
    };

demo源码地址

你可能感兴趣的:(android)