Can someone help me with the parameters to the And

2019-08-14 12:51发布

问题:

Please someone can explain to me the purpose of the source and dest parameters in android.text.InputFilter#filter?

I tried to read the docs but I am really confused. I am trying to use a regex to make an IP mask. Any help is appreciated.

I get it now. So, for example, if I have 123.42, then the user types 123.42d, I will have:

dest = 123.42  
source = 123.42d  
start = 5  
end = 6

InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter() 
    {
        public CharSequence filter(CharSequence source, int start, int end, Spanned  dest, int dstart, int dend) 
        {               
            String destTxt = dest.toString();
            String resultingTxt = destTxt.substring(0, dstart) +                           source.subSequence(start, end) + destTxt.substring(dend);                

            if(resultingTxt.equals("")) return "";

            int lastChar = resultingTxt.length() -1;

            if(String.valueOf(resultingTxt.charAt(lastChar)).matches("[^0-9.]"))
            {
                return "";
            }

            return null;
        }
    };

This isn't working though. Shouldn't this return me only the digits? It happens that depending on what the user type it returns me characters too.

回答1:

If you have an EditText and you assign an InputFilter to it, then everytime you change the text in there the filter() method will be called. Much like the onClick() method of a button.

Let's say you had the text "Hello Androi" in your EditText before editing it. If you press the D key on your virtual keyboard then the inputfilter is triggered and basically asked if it is ok to add a d.

In that case source is "Android", start is 6, end is 7 - That is your reference to the new Text.

dest would be "Androi" and refers to the old text in your EditText

So you get the new String and a position in that string (the 6,7) that you have to check if it is okay. If you would just get a single character (like the d) you could not decide if e.g. the number you just entered forms an ip adress. You need the whole text as a context in some cases.

If the new text is ok as is return null, if you want to skip a change return empty String (""), otherwise return the characters that replace the change.

So a simple example might be that:

/**
 * Simplified filter that should make everything uppercase
 * it's a demo and will probably not work
 *  - based on InputFilter.AllCaps
 */
public static class AllCaps implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {

        // create a buffer to store the edited character(s).
        char[] v = new char[end - start];

        // extract the characters between start and end into our buffer
        TextUtils.getChars(source, start, end, v, 0);

        // make the characters uppercase
        String s = new String(v).toUpperCase();

        // and return them
        return s;
    }
}

It's replacing every change with the uppercase version of it.