EditText TextWatch监听简单使用

TextWatch 接口方法如下:

方法执行顺序:beforeTextChanged() --> onTextChanged() --> afterTextChanged()

    new TextWatcher() {
        
        /**
         * This method is called to notify you that,
         * within s, the count characters beginning at
         * start are about to be replaced by
         * new text with length after.
         * It is an error to attempt to make changes to s from this callback
         *
         * 在原来字符串 s 中,从 start 位置开始
         * 正打算用 after 长度新字符串 替换 count 长度字符串
         * @param s
         * @param start
         * @param count
         * @param after
         */
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            
        }
        
        /**
         * This method is called to notify you that,
         * within s, the count characters beginning at start
         * have just replaced old text that had length before.
         * It is an error to attempt to make changes to s from this callback.
         *
         * 在改变后的字符串 s 中,从 start 位置开始
         * 将 before 位的字符串 替换成为  count 位的字符串
         *
         * @param s 改变前的字符串
         * @param start 开始发生替换初始index
         * @param before 被替换掉字符串的大小
         * @param count  替换字符串的大小
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            
        }
        
        /**
         * 改变完后 对结果进一步的修改 注意不要发生迭代死循环
         */
        @Override
        public void afterTextChanged(Editable s) {
            
        }
也可以通过打印日志的方式进行验证。

通过对count、after、before等进行判定实现字符串自动补充,自动删除多个等操作,增强交互。



你可能感兴趣的:(Android日常总结)