如何用空格代替逗号,当我使用" MultiAutoCompleteTextView" [英] How to replace the comma with a space when I use the "MultiAutoCompleteTextView"

查看:189
本文介绍了如何用空格代替逗号,当我使用" MultiAutoCompleteTextView"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做用MultiAutoCompleteTextView提示常用词一个简单的程序,当我

输入查询几个字母。

code:

  ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(
            这个,
            android.R.layout.simple_dropdown_item_1line,
            元);
    MultiAutoCompleteTextView的TextView =(MultiAutoCompleteTextView)findViewById(R.id.editText);
    textView.setAdapter(适配器);    textView.setTokenizer(新MultiAutoCompleteTextView.CommaTokenizer());    私有String []元=新的String [] {
    ABC,
    A B C D,
    ABCDE
    ABCDEF
    ABCDEFG
    HIJ
    HIJK
    hijkl
    hijklm
    hijklmn
    };

现在,当我输入'a'and选择ABCD,但结果成为ABCD。如何更换逗号空间?

感谢您!


解决方案

 公共类SpaceTokenizer实现分词{公众诠释findTokenStart(CharSequence的文字,INT光标){
INT I =光标;而(ⅰ大于0&放大器;&放大器;!text.charAt(I - 1)=''){
    一世 - ;
}
而(ⅰ&所述;光标&放大器;&放大器; text.charAt(ⅰ)==''){
    我++;
}返回我;
}公众诠释findTokenEnd(CharSequence的文字,INT光标){
INT I =光标;
INT LEN = text.length();而(I< LEN){
    如果(text.charAt(ⅰ)==''){
        返回我;
    }其他{
        我++;
    }
}返回LEN;
}公众的CharSequence terminateToken(CharSequence的文字){
INT I = text.length();而(ⅰ大于0&放大器;&放大器; text.charAt(I - 1)==''){
    一世 - ;
}如果(ⅰ大于0&放大器;&放大器; text.charAt(I - 1)==''){
    返回文本;
}其他{
    如果(文本的instanceof跨区){
        SpannableString SP =新SpannableString(文字+);
        TextUtils.copySpansFrom((跨区)文字,0,text.length()
                Object.class,SP,0);
        返回SP;
    }其他{
        返回文字+;
    }
}
}
}

I'm doing a simple program using "MultiAutoCompleteTextView" to prompt the common words when I

inputing 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 to a space?

Thank you!

解决方案

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 + " ";
    }
}
}
}

这篇关于如何用空格代替逗号,当我使用&QUOT; MultiAutoCompleteTextView&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆