MultiAutoCompleteTextView SpaceTokenizer
MultiAutoCompleteTextViewは複数の文字列をオートコンプリート出来る。デフォルトだと1つめの文字列をオートコンプリート入力してカンマを入力すると、2つ目の文字列を入力できるようになる。が、これはカンマ区切りしか対応していない。スペースと区切り文字としても利用したいところだけど、その場合はSpaceTokenizerを自作しないといけない。
public class SpaceTokenizer implements MultiAutoCompleteTextView.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 + " "; } } } } MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.comp); textView.setTokenizer(new SpaceTokenizer());
参考: