MultiAutoCompleteTextView

MultiAutoCompleteTextView

本文介绍了如何在android系统上MultiAutoCompleteTextView插入@符号,只有当显示下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MultiAutoCompleteTextView 。它工作正常。但我想说明建议下拉列表,只有当它的用户类型@(比如标记用户的Facebook应用程序)。我不知道该怎么做。这里是我的code:

  mChatbox =(MultiAutoCompleteTextView)findViewById(R.id.chatbox);ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_dropdown_item_1line,用户列表);
mChatBox.setAdapter(适配器);
mChatBox.setTokenizer(新SpaceTokenizer());公共类SpaceTokenizer实现MultiAutoCompleteTextView.Tokenizer {公众诠释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;
        }其他{
            返回文字+;
        }
    }
}


解决方案

我有我自己的解决方案。我创建它扩展自定义视图 MultiAutoCompleteTextView 并重写 performFiltering 在里面。检查第一个字符是@,那么后过滤下一字符。否则,用*代替字符以避免过滤。这里是我的code。

  @覆盖
保护无效performFiltering(CharSequence的文字,诠释开始,诠释年底,INT键code){
    如果(text.charAt(开始)=='@'){
        开始=开始+ 1;
    }其他{
        文字= text.subSequence(0开始);
        的for(int i =启动; I< END;我++){
               文本=文本+*;
        }
    }
    super.performFiltering(文字,开始,结束,重点code);
}

I have a MultiAutoCompleteTextView. It works fine. But I want to show suggestion dropdown only when user type @ on it (like tagging user in facebook app). I have no idea how to do it. Here is my code :

mChatbox = (MultiAutoCompleteTextView) findViewById(R.id.chatbox);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, userList);
mChatBox.setAdapter(adapter);
mChatBox.setTokenizer(new SpaceTokenizer());

public class SpaceTokenizer implements MultiAutoCompleteTextView.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 + " ";
        }
    }
}
解决方案

I got a solution by myself. I create custom view which extends MultiAutoCompleteTextView and override performFiltering in it. Check if first char is "@", then filter the next chars after it. Otherwise, replace chars with "*" to avoid filtering. Here is my code.

@Override
protected void performFiltering(CharSequence text, int start, int end, int keyCode) {
    if (text.charAt(start) == '@') {
        start = start + 1;
    } else {
        text = text.subSequence(0, start);
        for (int i = start; i < end; i++) {
               text = text + "*";
        }
    }
    super.performFiltering(text, start, end, keyCode);
}

这篇关于如何在android系统上MultiAutoCompleteTextView插入@符号,只有当显示下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:13