Android-Editext的InputFilter

InputFilter主要是对输入的文本进行过滤的,里面只有一个filter方法

//InputFilter接口,需要重写filter方法
public interface InputFilter
{
    /**
    * @param source 输入的文字
    * @param start 输入-0,删除-0
    * @param end 输入-source文字的长度,删除-0
    * @param dest 原先显示的内容
    * @param dstart 输入-原光标位置,删除-光标删除结束位置
    * @param dend  输入-原光标位置,删除-光标删除开始位置
    * @return
    */
    //主要重写这个方法
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend);

    /**
     * This filter will capitalize all the lower case letters that are added
     * through edits.
     */
    //接口内的静态内部类,,,输入小写转换成大写
    public static class AllCaps implements InputFilter {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (Character.isLowerCase(source.charAt(i))) {
                    char[] v = new char[end - start];
                    TextUtils.getChars(source, start, end, v, 0);
                    String s = new String(v).toUpperCase();

                    if (source instanceof Spanned) {
                        SpannableString sp = new SpannableString(s);
                        TextUtils.copySpansFrom((Spanned) source,
                                                start, end, null, sp, 0);
                        return sp;
                    } else {
                        return s;
                    }
                }
            }
            return null; // keep original
        }
    }
    /**
     * This filter will constrain edits not to make the length of the text
     * greater than the specified length.
     */
    // 长度过滤器,限制输入的长度
    public static class LengthFilter implements InputFilter {
        private final int mMax;
        public LengthFilter(int max) {
            mMax = max;
        }
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                int dstart, int dend) {
            int keep = mMax - (dest.length() - (dend - dstart));
            if (keep <= 0) {
                return "";
            } else if (keep >= end - start) {
                return null; // keep original
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }
        /**
         * @return the maximum length enforced by this input filter
         */
        public int getMax() {
            return mMax;
        }
    }
}

使用:

editext.setText(string);
editext.setFilters(filter);

Editext的setText函数:

 private void setText(CharSequence text, BufferType type,
                         boolean notifyBefore, int oldlen) {
        ......//其他代码
        int n = mFilters.length;
        //重点:设置文本需要提前经过设置的所有过滤器filter
        for (int i = 0; i < n; i++) {
            CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
            if (out != null) {
                text = out;
            }
        }

        if (notifyBefore) {
            if (mText != null) {
                oldlen = mText.length();
                sendBeforeTextChanged(mText, 0, oldlen, text.length());
            } else {
                sendBeforeTextChanged("", 0, 0, text.length());
            }
        }

        ......//其他代码
       //监听器TextWatcher的接口
        sendOnTextChanged(text, 0, oldlen, textLength);
        onTextChanged(text, 0, oldlen, textLength);

        notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);

        if (needEditableForNotification) {
            sendAfterTextChanged((Editable) text);
        } else {
            // Always notify AutoFillManager - it will return right away if autofill is disabled.
            notifyAutoFillManagerAfterTextChangedIfNeeded();
        }
        // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
        if (mEditor != null) mEditor.prepareCursorControllers();
    }
1.不让输入框输入内容
private InputFilter[] filter = new InputFilter[] {
            new InputFilter() {
                // 不让输入框输入内容
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    return null;
                }
            },

            /**这里限制输入的长度为5个字母*/
            new InputFilter.LengthFilter(5),
            /**输入小写转换成大写*/
            new InputFilter.AllCaps();
    };
2.只要你输入内容都会替换成“I LOVE YOU”,删除 - 正常删除
 @Override
       public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
              if (end > 0){
                   return "I LOVE YOU";
              }else {
                   return null;
              }
       }
3. 控制不让输入空格,不让输入数字大于13位(解决手机号输入问题)
@Override
 public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
       if(source.equals(" ") || source.toString().contentEquals("\n") || dstart == 13)return "";
        else return null;
 }
4.不让输入框接着输入内容(原来有内容,禁止输入)
 @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        return dest.subSequence(dstart,dend);
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,870评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,111评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,084评论 19 139
  • 一天中午,一个捡破烂的妇女,把捡来的破烂卖掉后,骑着三轮车往回走,经过一条小巷时,窜出一个歹徒。歹徒拿着刀,他用刀...
    麋鹿的方向盘阅读 4,503评论 1 0
  • 我在世上走着 带着我唯一的行囊 里面全是记忆 有我刚刚才放进去的 满怀激情热得发烫的 也有搁置得太久了的 被遗忘或...
    WYM_d8f5阅读 3,315评论 0 0

友情链接更多精彩内容