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.
char[] out = new char[end - start]; // reserve enough space for whole string
int outidx = 0;
boolean changed = false;
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
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isAllowed(c)) {
// Character allowed. Add it to the sequence.
out[outidx++] = c;
} else {
if (mAppendInvalid) out[outidx++] = c;
else changed = true; // we changed the original string
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();
if (!changed) {
return null;
}
String s = new String(out, 0, outidx);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
TextUtils.copySpansFrom((Spanned) source,
start, end, null, sp, 0);
return sp;
} else {
return s;
}
|