Multiautocompletetextview,仅当用户在“@"键后按下某个键时才显示自动完成下拉菜单(如 FB 应用程序中提到的) [英] Multiautocompletetextview, Show autocomplete drop down only when user presses a key after '@' key (like mention in FB app)
问题描述
我让它与自定义的@"标记器一起工作.但是第一次自动完成失败.我的代码作为一个逗号标记器工作,我得到任何字符的建议和下一个建议仅在逗号之后(在我的例子中是 '@').这是我的代码.
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);
}
}
推荐答案
如果你想用'@'字符进行提及查询,你可以在onTextChanged查询如下:
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
}
}
}
注意:您也可以用字符#"进行hashTag查询,将其替换为
"@([A-Za-z0-9_-]+)"
"@([A-Za-z0-9_-]+)"
这篇关于Multiautocompletetextview,仅当用户在“@"键后按下某个键时才显示自动完成下拉菜单(如 FB 应用程序中提到的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!