Multiautocompletetextview, Show autocomplete drop

2019-02-19 09:19发布

I got it working with custom '@' tokenizer. But it fails for the first autocompletion. My code works as a comma tokenizer where I get suggestion for any character and next suggestion only after a comma(it's '@' in my case). Here's my code.

         String[] str={"Andoid","Jelly Bean","Froyo",
            "Ginger Bread","Eclipse Indigo","Eclipse Juno"};

    editEmojicon.setTokenizer(new 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+" ";
                }
            }
        }
    });

    ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line,str);

    editEmojicon.setThreshold(1);
    editEmojicon.setAdapter(adp);

Saw this post here But didn't work. I will be grateful if someone could help me with this.

EmojiconEditText.java

public class EmojiconEditText extends MultiAutoCompleteTextView {
private int mEmojiconSize;

public EmojiconEditText(Context context) {
    super(context);
    mEmojiconSize = (int) getTextSize();

}

public EmojiconEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

public EmojiconEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

private void init(AttributeSet attrs) {
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon);
    mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize());
    a.recycle();
    setText(getText());
}

@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
    EmojiconHandler.addEmojis(getContext(), getText(), mEmojiconSize);
}

/**
 * Set the size of emojicon in pixels.
 */
public void setEmojiconSize(int pixels) {
    mEmojiconSize = pixels;
}

/** For Mention System / Added by midhun  **/

@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);
}

}

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-19 09:46

https://github.com/linkedin/Spyglass check this out , having all the use cases for mentions

查看更多
成全新的幸福
3楼-- · 2019-02-19 09:48

If you want to do mention query with the character '@', you can do query onTextChanged as below:

        @Override
        public void onTextChanged(CharSequence s, int start, int before, final int count) {
            if (s.length() > 0) {
                // Todo: query mentions
                Matcher mentionMatcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(s.toString());
                // while matching
                while (mentionMatcher.find()) {
                   yourSearchText = s.toString().substring(mentionMatcher.start() + 1, mentionMatcher.end());
                  // do query with yourSearchText below
                }
           }
       }

Note: you also can do hashTag query with the character '#' by replace it in

"@([A-Za-z0-9_-]+)"

查看更多
登录 后发表回答