Android和CommaTokenizer [英] Android and the CommaTokenizer
本文介绍了Android和CommaTokenizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个标记生成器(用于AutoCompleteTextview),它可以做到以下几点:
-
当由空白字符分隔
- 在两个词必须承认这样
- 在两个词也必须在被换行分隔承认的(输入pressed)
1)在工作,但我怎么能做到2?
公共类SpaceTokenizer实现分词{
@覆盖
公众诠释findTokenStart(CharSequence的文字,诠释光标){
INT I =光标;
而(ⅰ大于0&安培;&安培;!(text.charAt(ⅰ - 1)='')){
一世 - ;
}
而(ⅰ&其中;光标&安培;及(text.charAt(ⅰ)==''|| text.charAt(ⅰ)=='\ N')){
我++;
}
返回我;
}
@覆盖
公众诠释findTokenEnd(CharSequence的文字,诠释光标){
INT I =光标;
INT的len = text.length();
而(I< LEN){
如果(text.charAt(一)==''|| text.charAt(一)=='\ N'){
返回我;
} 其他 {
我++;
}
}
返回的len;
}
@覆盖
公共CharSequence的terminateToken(CharSequence的文字){
INT I = text.length();
而(ⅰ大于0&安培;及(text.charAt(ⅰ - 1)==''|| text.charAt(ⅰ - 1)=='\ N')){
一世 - ;
}
如果(ⅰ大于0&安培;及(text.charAt(ⅰ - 1)==''|| text.charAt(ⅰ - 1)=='\ N')){
返回文本;
} 其他 {
如果(文本的instanceof跨区){
SpannableString SP =新SpannableString(文字+);
TextUtils.copySpansFrom((跨区)文本,0,text.length()
Object.class,藻,0);
返回SP;
} 其他 {
返回文本+;
}
}
}
}
解决方案
您应该能够做到这一点: text.charAt(一)==''|| text.charAt(一)=='\ N'
或 I-1
在适当情况下。
I need a Tokenizer (for the AutoCompleteTextview) which can do the following:
- Two words must be recognized as such when separated by a blank character
- Two words must also be recognized as such when separated by a newline ("Enter" pressed)
1) is working, but how can I accomplish 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 + " ";
}
}
}
}
解决方案
You should be able to do this: text.charAt(i) == ' ' || text.charAt(i) == '\n'
or i-1
where appropriate.
这篇关于Android和CommaTokenizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文