public java.lang.CharSequence | filter(java.lang.CharSequence source, int start, int end, Spanned dest, int dstart, int dend)This method is called when the buffer is going to replace the
range dstart … dend of dest
with the new text from the range start … end
of source . Returns the CharSequence that we want
placed there instead, including an empty string
if appropriate, or null to accept the original
replacement. Be careful to not to reject 0-length replacements,
as this is what happens when you delete text.
onStart();
// Scan through beginning characters in dest, calling onInvalidCharacter()
// for each invalid character.
for (int i = 0; i < dstart; i++) {
char c = dest.charAt(i);
if (!isAllowed(c)) onInvalidCharacter(c);
}
// Scan through changed characters rejecting disallowed chars
SpannableStringBuilder modification = null;
int modoff = 0;
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isAllowed(c)) {
// Character allowed.
modoff++;
} else {
if (mAppendInvalid) {
modoff++;
} else {
if (modification == null) {
modification = new SpannableStringBuilder(source, start, end);
modoff = i - start;
}
modification.delete(modoff, modoff + 1);
}
onInvalidCharacter(c);
}
}
// Scan through remaining characters in dest, calling onInvalidCharacter()
// for each invalid character.
for (int i = dend; i < dest.length(); i++) {
char c = dest.charAt(i);
if (!isAllowed(c)) onInvalidCharacter(c);
}
onStop();
// Either returns null if we made no changes,
// or what we wanted to change it to if there were changes.
return modification;
|