Android-Editext的InputFilter

InputFilter主要是對輸入的文本進(jìn)行過濾的帜讲,里面只有一個filter方法

//InputFilter接口开皿,需要重寫filter方法
public interface InputFilter
{
    /**
    * @param source 輸入的文字
    * @param start 輸入-0懒浮,刪除-0
    * @param end 輸入-source文字的長度奔坟,刪除-0
    * @param dest 原先顯示的內(nèi)容
    * @param dstart 輸入-原光標(biāo)位置叨恨,刪除-光標(biāo)刪除結(jié)束位置
    * @param dend  輸入-原光標(biāo)位置柳刮,刪除-光標(biāo)刪除開始位置
    * @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.
     */
    //接口內(nèi)的靜態(tài)內(nèi)部類,痒钝,秉颗,輸入小寫轉(zhuǎn)換成大寫
    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函數(shù):

 private void setText(CharSequence text, BufferType type,
                         boolean notifyBefore, int oldlen) {
        ......//其他代碼
        int n = mFilters.length;
        //重點(diǎn):設(shè)置文本需要提前經(jīng)過設(shè)置的所有過濾器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());
            }
        }

        ......//其他代碼
       //監(jiān)聽器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.不讓輸入框輸入內(nèi)容
private InputFilter[] filter = new InputFilter[] {
            new InputFilter() {
                // 不讓輸入框輸入內(nèi)容
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    return null;
                }
            },

            /**這里限制輸入的長度為5個字母*/
            new InputFilter.LengthFilter(5),
            /**輸入小寫轉(zhuǎn)換成大寫*/
            new InputFilter.AllCaps();
    };
2.只要你輸入內(nèi)容都會替換成“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. 控制不讓輸入空格蚕甥,不讓輸入數(shù)字大于13位(解決手機(jī)號輸入問題)
@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.不讓輸入框接著輸入內(nèi)容(原來有內(nèi)容,禁止輸入)
 @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        return dest.subSequence(dstart,dend);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末栋荸,一起剝皮案震驚了整個濱河市菇怀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌晌块,老刑警劉巖爱沟,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異匆背,居然都是意外死亡呼伸,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門钝尸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜂大,“玉大人,你說我怎么就攤上這事蝶怔∧唐郑” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵踢星,是天一觀的道長澳叉。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么成洗? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任五督,我火速辦了婚禮,結(jié)果婚禮上瓶殃,老公的妹妹穿的比我還像新娘充包。我一直安慰自己,他們只是感情好遥椿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布基矮。 她就那樣靜靜地躺著,像睡著了一般冠场。 火紅的嫁衣襯著肌膚如雪家浇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天碴裙,我揣著相機(jī)與錄音钢悲,去河邊找鬼。 笑死舔株,一個胖子當(dāng)著我的面吹牛莺琳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播载慈,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼芦昔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了娃肿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤珠十,失蹤者是張志新(化名)和其女友劉穎料扰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體焙蹭,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晒杈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了孔厉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拯钻。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖撰豺,靈堂內(nèi)的尸體忽然破棺而出粪般,到底是詐尸還是另有隱情,我是刑警寧澤污桦,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布亩歹,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏小作。R本人自食惡果不足惜亭姥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望顾稀。 院中可真熱鬧达罗,春花似錦、人聲如沸静秆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诡宗。三九已至滔蝉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間塔沃,已是汗流浹背蝠引。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛀柴,地道東北人螃概。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像鸽疾,于是被迫代替她去往敵國和親吊洼。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程制肮,因...
    小菜c閱讀 6,419評論 0 17
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,139評論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理冒窍,服務(wù)發(fā)現(xiàn),斷路器豺鼻,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • 一天中午儒飒,一個撿破爛的婦女谬莹,把撿來的破爛賣掉后,騎著三輪車往回走桩了,經(jīng)過一條小巷時附帽,竄出一個歹徒。歹徒拿著刀井誉,他用刀...
    麋鹿的方向盤閱讀 915評論 1 0
  • 我在世上走著 帶著我唯一的行囊 里面全是記憶 有我剛剛才放進(jìn)去的 滿懷激情熱得發(fā)燙的 也有擱置得太久了的 被遺忘或...
    WYM_d8f5閱讀 449評論 0 0