| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.text.Editable; |
| 21 |
import android.text.SpannableString; |
| 22 |
import android.text.Spanned; |
| 23 |
import android.text.TextUtils; |
| 24 |
import android.text.method.QwertyKeyListener; |
| 25 |
import android.util.AttributeSet; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public class MultiAutoCompleteTextView extends AutoCompleteTextView { |
| 58 |
private Tokenizer mTokenizer; |
| 59 |
|
| 60 |
public MultiAutoCompleteTextView(Context context) { |
| 61 |
this(context, null); |
| 62 |
} |
| 63 |
|
| 64 |
public MultiAutoCompleteTextView(Context context, AttributeSet attrs) { |
| 65 |
this(context, attrs, com.android.internal.R.attr.autoCompleteTextViewStyle); |
| 66 |
} |
| 67 |
|
| 68 |
public MultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 69 |
this(context, attrs, defStyleAttr, 0); |
| 70 |
} |
| 71 |
|
| 72 |
public MultiAutoCompleteTextView( |
| 73 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 74 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 75 |
} |
| 76 |
|
| 77 |
void finishInit() { } |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void setTokenizer(Tokenizer t) { |
| 84 |
mTokenizer = t; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
protected void performFiltering(CharSequence text, int keyCode) { |
| 95 |
if (enoughToFilter()) { |
| 96 |
int end = getSelectionEnd(); |
| 97 |
int start = mTokenizer.findTokenStart(text, end); |
| 98 |
|
| 99 |
performFiltering(text, start, end, keyCode); |
| 100 |
} else { |
| 101 |
dismissDropDown(); |
| 102 |
|
| 103 |
Filter f = getFilter(); |
| 104 |
if (f != null) { |
| 105 |
f.filter(null); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Override |
| 118 |
public boolean enoughToFilter() { |
| 119 |
Editable text = getText(); |
| 120 |
|
| 121 |
int end = getSelectionEnd(); |
| 122 |
if (end < 0 || mTokenizer == null) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
int start = mTokenizer.findTokenStart(text, end); |
| 127 |
|
| 128 |
if (end - start >= getThreshold()) { |
| 129 |
return true; |
| 130 |
} else { |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@Override |
| 140 |
public void performValidation() { |
| 141 |
Validator v = getValidator(); |
| 142 |
|
| 143 |
if (v == null || mTokenizer == null) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
Editable e = getText(); |
| 148 |
int i = getText().length(); |
| 149 |
while (i > 0) { |
| 150 |
int start = mTokenizer.findTokenStart(e, i); |
| 151 |
int end = mTokenizer.findTokenEnd(e, start); |
| 152 |
|
| 153 |
CharSequence sub = e.subSequence(start, end); |
| 154 |
if (TextUtils.isEmpty(sub)) { |
| 155 |
e.replace(start, i, ""); |
| 156 |
} else if (!v.isValid(sub)) { |
| 157 |
e.replace(start, i, |
| 158 |
mTokenizer.terminateToken(v.fixText(sub))); |
| 159 |
} |
| 160 |
|
| 161 |
i = start; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
protected void performFiltering(CharSequence text, int start, int end, |
| 172 |
int keyCode) { |
| 173 |
getFilter().filter(text.subSequence(start, end), this); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
@Override |
| 190 |
protected void replaceText(CharSequence text) { |
| 191 |
clearComposingText(); |
| 192 |
|
| 193 |
int end = getSelectionEnd(); |
| 194 |
int start = mTokenizer.findTokenStart(getText(), end); |
| 195 |
|
| 196 |
Editable editable = getText(); |
| 197 |
String original = TextUtils.substring(editable, start, end); |
| 198 |
|
| 199 |
QwertyKeyListener.markAsReplaced(editable, start, end, original); |
| 200 |
editable.replace(start, end, mTokenizer.terminateToken(text)); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public CharSequence getAccessibilityClassName() { |
| 205 |
return MultiAutoCompleteTextView.class.getName(); |
| 206 |
} |
| 207 |
|
| 208 |
public static interface Tokenizer { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public int findTokenStart(CharSequence text, int cursor); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public int findTokenEnd(CharSequence text, int cursor); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public CharSequence terminateToken(CharSequence text); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public static class CommaTokenizer implements Tokenizer { |
| 233 |
public int findTokenStart(CharSequence text, int cursor) { |
| 234 |
int i = cursor; |
| 235 |
|
| 236 |
while (i > 0 && text.charAt(i - 1) != ',') { |
| 237 |
i--; |
| 238 |
} |
| 239 |
while (i < cursor && text.charAt(i) == ' ') { |
| 240 |
i++; |
| 241 |
} |
| 242 |
|
| 243 |
return i; |
| 244 |
} |
| 245 |
|
| 246 |
public int findTokenEnd(CharSequence text, int cursor) { |
| 247 |
int i = cursor; |
| 248 |
int len = text.length(); |
| 249 |
|
| 250 |
while (i < len) { |
| 251 |
if (text.charAt(i) == ',') { |
| 252 |
return i; |
| 253 |
} else { |
| 254 |
i++; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return len; |
| 259 |
} |
| 260 |
|
| 261 |
public CharSequence terminateToken(CharSequence text) { |
| 262 |
int i = text.length(); |
| 263 |
|
| 264 |
while (i > 0 && text.charAt(i - 1) == ' ') { |
| 265 |
i--; |
| 266 |
} |
| 267 |
|
| 268 |
if (i > 0 && text.charAt(i - 1) == ',') { |
| 269 |
return text; |
| 270 |
} else { |
| 271 |
if (text instanceof Spanned) { |
| 272 |
SpannableString sp = new SpannableString(text + ", "); |
| 273 |
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), |
| 274 |
Object.class, sp, 0); |
| 275 |
return sp; |
| 276 |
} else { |
| 277 |
return text + ", "; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |