MultiAutoCompleteTextView自动联想查找

private MultiAutoCompleteTextView mAddEmaiAddressEditText = null;

// 初始化

mAddEmaiAddressEditText = (MultiAutoCompleteTextView) findViewById(R.id.email_add_address_edittext);
        List<String> emails = getAllEmails(); // 获取已保存所有邮箱地址
        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, R.layout.email_list_item,
                emails);
        mAddEmaiAddressEditText.setAdapter(mAdapter);
        mAddEmaiAddressEditText.setTokenizer(new MySemicolonTokenizer());


//重点在此,系统默认的是以“,”为分隔符,我项目中需要用到";"为分隔符

/**
     * This simple Tokenizer can be used for lists where the items are
     * separated by a Semicolon and one or more spaces.
     */
    public static class MySemicolonTokenizer 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 + "; ";
                }
            }
        }
    }


当然如果你想用其他分隔符也可以,就是把你用的分隔符替换上面函数中的“;”就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值