How to replace the comma with a space when I use t

2019-01-16 20:01发布

I'm doing a simple program using MultiAutoCompleteTextView to prompt the common words when I input several letters.

code:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_dropdown_item_1line, 
            ary);
    MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.editText);
    textView.setAdapter(adapter);

    textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    private String[] ary = new String[] {
       "abc",
       "abcd",
       "abcde",
       "abcdef",
       "abcdefg",
       "hij",
       "hijk",
       "hijkl",
       "hijklm",
       "hijklmn",
    };

Now,when I input 'a' and choose "abcd" but the result become to "abcd,". How to replace the comma with a space?

Thank you!

3条回答
虎瘦雄心在
2楼-- · 2019-01-16 20:29
public class SpaceTokenizer 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 + " ";
    }
}
}
}
查看更多
唯我独甜
3楼-- · 2019-01-16 20:39

Check my question/answer

How to replace MultiAutoCompleteTextView drop down list

you will find a SpaceTokenizer class

查看更多
神经病院院长
4楼-- · 2019-01-16 20:44

The way to do it would be to implement your own Tokenizer. The reason the comma comes up is because you're using CommaTokenizer, which is designed to do exactly that. You can also look at the source code for CommaTokenizer if you need a reference for how to implement your own SpaceTokenizer.

查看更多
登录 后发表回答