Can Someone Help Me With The Parameters To The Android Inputfilter "filter" Method? (plus Regex)
Solution 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
*/publicstaticclassAllCaps 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 = newchar[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 = newString(v).toUpperCase();
// and return themreturn s;
}
}
It's replacing every change with the uppercase version of it.
Post a Comment for "Can Someone Help Me With The Parameters To The Android Inputfilter "filter" Method? (plus Regex)"