在multiautocompletetextview多个标记生成器

问题描述 投票:0回答:1

我曾试图在MultiAutoCompleteTextView.For例如,如果用户输入逗号设置多个标记生成器或分号应该叫我的适配器。

我至今尝试过

public class CommaTokenizer implements MultiAutoCompleteTextView.Tokenizer {
    public int findTokenStart(CharSequence text, int cursor) {
        int i = cursor;

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

但它不是working.My适配器没有得到called.Any帮助将不胜感激。

java android multiautocompletetextview
1个回答
0
投票

它正在与下面的解决方案。

public class CommaTokenizer implements MultiAutoCompleteTextView.Tokenizer {
    public int findTokenStart(CharSequence text, int cursor) {
        int i = cursor;

        while (i > 0) {
            if ((text.charAt(i - 1) == ';') || (text.charAt(i - 1) == ',')) {
                return i - 1;
            }
            i--;
            return i;
        }

        return i;
    }

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

        while (i < len) {
            if ((text.charAt(i) == ';') || (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) == ';') || (text.charAt(i - 1) == ','))) {
            return text;
        } else {
            if (text instanceof Spanned) {
                SpannableString sp = new SpannableString("");
                if (i > 0 && (text.charAt(i - 1) == ';')) {
                    sp = new SpannableString(text + ";");
                } else if (i > 0 && (text.charAt(i - 1) == ',')) {
                    sp = new SpannableString(text + ",");
                }
                TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                        Object.class, sp, 0);
                return sp;
            } else {
                if (i > 0 && (text.charAt(i - 1) == ',')) {
                    return text + ",";
                } else {
                    return text + ";";
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.