我需要一个 Tokenizer(用于 AutoCompleteTextview),它可以执行以下操作:
- 当两个单词以空白字符分隔时,必须按原样进行识别
- 当用换行符分隔时,两个单词也必须被识别(按下“Enter”键)
1)正在工作,但我怎样才能完成2?
public class SpaceTokenizer implements Tokenizer {
@Override
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && (text.charAt(i - 1) != ' ')) {
i--;
}
while (i < cursor && (text.charAt(i) == ' ' || text.charAt(i) == '\n')) {
i++;
}
return i;
}
@Override
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ' || text.charAt(i) == '\n') {
return i;
} else {
i++;
}
}
return len;
}
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
i--;
}
if (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
}
您应该能够执行以下操作:text.charAt(i) == ' ' || text.charAt(i) == '\n'
or i-1
在适当情况下。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)