Understand inputfilter of edittext

Source: Internet
Author: User

In the process of using edittext, you may encounter disable (input prohibited) requirements. Using setenabled (false) directly is invalid. You can set inputfilter to enable online search.

myEditText.setInputFilters(new InputFilter[]{   new InputFilter() {      @Override      filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {         return dest.subSequence(dstart, dend);      }   }});

So how is disable implemented? Let's take a look at the introduction of the inputfilter interface implementation class.

Inputfilter. allcaps
-- This filter will capitalize all the lower case letters that are added through edits
Inputfilter. lengthfillter
-- This filter will constrain edits not to make the length of the text greater than the specified length

Numberkeylistener
-- For numeric text entry
Datekeylistener
-- For entering dates in a text field
Datetimekeylistener
-- For entering dates and times in the same text field
Dialerkeylistener
-- For dialing-only text entry
Digitskeylistener
-- For digits-only text entry
Timekeylistener
-- For entering times in a text field

Loginfilter
-- Abstract class for filtering login-related text (user names and passwords)
Loginfilter. passwordfiltergmail
-- This filter is compatible with Gmail passwords which restricts characters to the Latin-1 (ISO8859-1) Char set
Loginfilter. usernamefiltergmail
-- This filter rejects characters in the user name that are not compatible with Gmail account creation
Loginfilter. usernamefiltergeneric
-- This filter rejects characters in the user name that are not compatible with Google Login

As you can see, inputfilter mainly filters user (input) text. This interface has only one filter method.

Let's take a look at the implementation principle of inputfilter. allcaps.

    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        }    }

From the Code, it is not difficult to find that when the string source contains lower-case letters, It is converted to upper-case letters and then returned, so as to implement allcaps.

Let's take a look at the implementation principle of inputfilter. lengthfillter.

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

Here, mMax is the maximum text length, and keep is the difference between mMax and the current text length.

  1. When the keep value is less than 0, that is, if the length of the text exceeds the upper limit ""
  2. If the length of the text does not exceed the upper limit, null is returned to keep the text unchanged (keep original)
  3. The last else can be ignored for the time being.

Let's take a look at how edittext uses this inputfilter. In fact, inputfilter is used in textview, In the settext () method of textview.

        int n = mFilters.length;        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;            }        }...        sendOnTextChanged(text, 0, oldlen, textLength);        onTextChanged(text, 0, oldlen, textLength);

Traverse all inputfilters. If the filter result is not null, update the text variable. This means that if the filter returns NULL, the original text remains unchanged (as shown above in inputfilter. lengthfilter ). The result of all inputfilters is the final text displayed in textview.

After learning about the inputfilter principle, you will know how to make "Hands and feet" on textview/edittext. For example

  1. Do not respond to all special characters entered (@#$ % ^ &)
  2. Display up to 100 uppercase letters can be entered (use the combination of inputfilter. lengthfilter and inputfilter. allcaps)
  3. Automatically replaces one character with another.
Let's look back at the inputfilter of disable edittext. For edittext, the last three parameters that call the filter () are always empty_spanned, 0, 0. When the filter is executed, it is equivalent to executing empty_spanned.subsequence (0, 0) and returning "" Forever, thus implementing disable.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.