How to replace the comma with a space when I use the “MultiAutoCompleteTextView”

a 夏天 提交于 2019-11-27 18:34:55
public class SpaceTokenizer implements Tokenizer {

public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;

while (i > 0 && text.charAt(i - 1) != ' ') {
    i--;
}
while (i < cursor && text.charAt(i) == ' ') {
    i++;
}

return i;
}

public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();

while (i < len) {
    if (text.charAt(i) == ' ') {
        return i;
    } else {
        i++;
    }
}

return len;
}

public CharSequence terminateToken(CharSequence text) {
int i = text.length();

while (i > 0 && text.charAt(i - 1) == ' ') {
    i--;
}

if (i > 0 && text.charAt(i - 1) == ' ') {
    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 + " ";
    }
}
}
}

The way to do it would be to implement your own Tokenizer. The reason the comma comes up is because you're using CommaTokenizer, which is designed to do exactly that. You can also look at the source code for CommaTokenizer if you need a reference for how to implement your own SpaceTokenizer.

vsm

Check my question/answer

How to replace MultiAutoCompleteTextView drop down list

you will find a SpaceTokenizer class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!