分隔符问题
像分号的分隔符怎样设置?
像分号的分隔符怎样设置?
2016-07-28
默认逗号分隔符,可以使用下面方法自定义分隔符,以‘;’为例
添加这个SemicolonTokenizer类到项目中,并将macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());改为macTextView.setTokenizer(new SemicolonTokenizer(';'));即可
附录:SemicolonTokenizer类代码
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView.Tokenizer;
public class SemicolonTokenizer implements Tokenizer {
private char charS;
private String mSTring;
public SemicolonTokenizer(char charS) {
this.charS = charS;
mSTring = String.valueOf(charS);
}
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != charS) {
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) == charS) {
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) == charS) {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + mSTring);
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
return sp;
} else {
return text + mSTring;
}
}
}
}
2016-05-02
举报