【转载】为MultiAutoCompleteTextView.setTokenizer(..)指定分格符号
|
|
文件 SemicolonTokenizer.java
|
02 |
03 |
import android.text.SpannableString; |
04 |
import android.text.Spanned; |
05 |
import android.text.TextUtils; |
06 |
import android.widget.MultiAutoCompleteTextView.Tokenizer; |
07 |
08 |
public class SemicolonTokenizer implements Tokenizer { |
09 |
private char charS; |
10 |
private String mSTring; |
11 |
12 |
public SemicolonTokenizer(char charS) { |
13 |
this.charS = charS; |
14 |
mSTring = String.valueOf(charS); |
15 |
} |
16 |
17 |
public int findTokenStart(CharSequence text, int cursor) { |
18 |
int i = cursor; |
19 |
20 |
while (i > 0 && text.charAt(i - 1) != charS) { |
21 |
i--; |
22 |
} |
23 |
while (i < cursor && text.charAt(i) == ' ') { |
24 |
i++; |
25 |
} |
26 |
27 |
return i; |
28 |
} |
29 |
30 |
public int findTokenEnd(CharSequence text, int cursor) { |
31 |
int i = cursor; |
32 |
int len = text.length(); |
33 |
34 |
while (i < len) { |
35 |
if (text.charAt(i) == charS) { |
36 |
return i; |
37 |
} else { |
38 |
i++; |
39 |
} |
40 |
} |
41 |
42 |
return len; |
43 |
} |
44 |
45 |
public CharSequence terminateToken(CharSequence text) { |
46 |
int i = text.length(); |
47 |
48 |
while (i > 0 && text.charAt(i - 1) == ' ') { |
49 |
i--; |
50 |
} |
51 |
52 |
if (i > 0 && text.charAt(i - 1) == charS) { |
53 |
return text; |
54 |
} else { |
55 |
if (text instanceof Spanned) { |
56 |
SpannableString sp = new SpannableString(text + mSTring); |
57 |
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0); |
58 |
return sp; |
59 |
} else { |
60 |
return text + mSTring; |
61 |
} |
62 |
} |
63 |
} |
64 |
文件MainActivity.java
|


浙公网安备 33010602011771号