我想我已经最终找到了工作中的困境,但必须更深入地尝试以确定这是否存在一些错误或错误。
public abstract class TextListener implements InputFilter {
public abstract CharSequence removeStr(CharSequence removeChars, int startPos);
public abstract CharSequence insertStr(CharSequence newChars, int startPos);
public abstract CharSequence updateStr(CharSequence oldChars, int startPos, CharSequence newChars);
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
CharSequence returnStr = source;
String curStr = dest.subSequence(dstart, dend).toString();
String newStr = source.toString();
int length = end - start;
int dlength = dend - dstart;
if (dlength > 0 && length == 0) {
// Case: Remove chars, Simple
returnStr = TextListener.this.removeStr(dest.subSequence(dstart, dend), dstart);
} else if (length > 0 && dlength == 0) {
// Case: Insert chars, Simple
returnStr = TextListener.this.insertStr(source.subSequence(start, end), dstart);
} else if (curStr.length() > newStr.length()) {
// Case: Remove string or replace
if (curStr.startsWith(newStr)) {
// Case: Insert chars, by append
returnStr = TextUtils.concat(curStr.subSequence(0, newStr.length()), TextListener.this.removeStr(curStr.subSequence(newStr.length(), curStr.length()), dstart + curStr.length()));
} else {
// Case Replace chars.
returnStr = TextListener.this.updateStr(curStr, dstart, newStr);
}
} else if (curStr.length() < newStr.length()) {
// Case: Append String or rrepace.
if (newStr.startsWith(curStr)) {
// Addend, Insert
returnStr = TextUtils.concat(curStr, TextListener.this.insertStr(newStr.subSequence(curStr.length(), newStr.length()), dstart + curStr.length()));
} else {
returnStr = TextListener.this.updateStr(curStr, dstart, newStr);
}
} else {
// No update os str...
}
// If the return value is same as the source values, return the source value.
return TextUtils.equals(source, returnStr) ? source : returnStr;
}
}
从这个代码,我可以轻松防止通过查找文本的选择部分编辑是在我尝试编辑的文本。
你说**“但这看起来不可靠”**请解释原因。 –
是不是真的不可靠,但这给了我洞字而不是输入字符,如果我在电话中输入“你好”,这将首先给我“H”,“他”,“Hel”,“地狱” “这对我来说不是我想要的。我现在试着将它翻译成我将要做的,我已经发现'InputFilter'用于编辑/拒绝编辑,但在这里也有同样的问题。 –