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);

你可能感兴趣的:(Android-Editext的InputFilter)