Created
November 10, 2018 12:50
-
-
Save ron4stoppable/52410d4c8b8458f919908d54ca09cffc to your computer and use it in GitHub Desktop.
A tokenizer class for MultiAutoCompleteTextView, with text seperated with space.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.text.SpannableString; | |
import android.text.Spanned; | |
import android.text.TextUtils; | |
import android.widget.MultiAutoCompleteTextView; | |
public class SpaceTokenizer implements MultiAutoCompleteTextView.Tokenizer { | |
@Override | |
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; | |
} | |
@Override | |
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; | |
} | |
@Override | |
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 + " "; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment