2

我让它与自定义'@'标记器一起使用。但它在第一次自动完成时失败了。我的代码作为逗号标记器工作,我只在逗号之后获得任何字符的建议和下一个建议(在我的情况下是'@')。这是我的代码。

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

在这里看到了这篇文章但是没有用。如果有人可以帮助我,我将不胜感激。

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

}

4

2 回答 2

1

如果您想使用字符“@”进行提及查询,您可以查询 onTextChanged,如下所示:

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

注意:您也可以使用字符“#”进行 hashTag 查询,方法是将其替换为

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

于 2016-09-21T04:43:32.237 回答
-2

https://github.com/linkedin/Spyglass 看看这个,有提到的所有用例

于 2015-12-16T06:59:51.123 回答