private MultiAutoCompleteTextView mAddEmaiAddressEditText = null;
// 初始化
mAddEmaiAddressEditText = (MultiAutoCompleteTextView) findViewById(R.id.email_add_address_edittext);
List<String> emails = getAllEmails(); // 获取已保存所有邮箱地址
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, R.layout.email_list_item,
emails);
mAddEmaiAddressEditText.setAdapter(mAdapter);
mAddEmaiAddressEditText.setTokenizer(new MySemicolonTokenizer());
//重点在此,系统默认的是以“,”为分隔符,我项目中需要用到";"为分隔符
/**
* This simple Tokenizer can be used for lists where the items are
* separated by a Semicolon and one or more spaces.
*/
public static class MySemicolonTokenizer 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 + "; ";
}
}
}
}
当然如果你想用其他分隔符也可以,就是把你用的分隔符替换上面函数中的“;”就可以了。