Android: What do the parameters in InputFilter.filter mean and when is filter actually called?

774 Views Asked by At

I am trying to understand the six parameters in the InputFilter.filter() method, so I have written the following class.

final class MyFilter implements InputFilter {

static final String TAG = "MyFilter";

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    Log.d(TAG, "source = \"" + source + "\"");
    Log.d(TAG, "start = " + start);
    Log.d(TAG, "end = " + end);
    Log.d(TAG, "dest = \"" + dest + "\"");
    Log.d(TAG, "dstart = " + dstart);
    Log.d(TAG, "dend = " + dend);
    return null;
}}

I have included an EditText in my activity and written:

editText.setFilters(new InputFilter[] { new MyFilter() });

When I modify the text in the EditText I get some results I can't understand at all.

When I write "a", filter() is called once with parameters "a", 0, 1, "", 0, 0.

When I add 'b' so it says "ab", filter() is called twice, first with the parameters "ab", 0, 2, "a", 0, 1 and then with the parameters "ab", 0, 2, "ab", 0, 2.

When I add 'c' so it says "abc", filter() is called only once with parameters "abc", 0, 3, "ab", 0, 2.

When I add 'd' so it says "abcd", filter() is called twice, the first time with parameters "abcd", 0, 4, "abc", 0, 3 and then with parameters "abcd", 0, 4, "abcd", 0, 4.

When I add subsequent characters, filter() is only called once for each.

What is going on? Why are strings of length 2 and 4 treated differently to strings of length 1, 3, 5, 6, 7, 8...?

Also why are there 6 parameters, when 2 seems to be enough?

Thanks in advance to anyone who can explain this.

0

There are 0 best solutions below