How to show dropdown only when inserting @ character on MultiAutoCompleteTextView in android

南楼画角 提交于 2019-12-08 11:02:43

问题


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 + " ";
        }
    }
}

回答1:


Create a custom Text view extending MultiAutoCompleteTextView -> override enoughToFilter() -> set the threshold to 0 (the bold variable in the below given code) :

public boolean enoughToFilter() {
Editable text = getText();
int end = getSelectionEnd();
if (end < 0 || mTokenizer == null) {
  return false;
}
int start = mTokenizer.findTokenStart(text, end);
if (end - start >= mThreshold && start != -1) {
  return true;
} else {
  return false;
}

}

Using this code you'll see the auto suggested list on press of @




回答2:


If you want to detect your string starts with '@' for mention (tag) someone or '#' for hashTag, then do query or filter with it, you could follow this code belows:

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

It references from the link Multiautocompletetextview, Show autocomplete drop down only when user presses a key after '@' key (like mention in FB app) please scroll down to find @Phuong Sala answer.




回答3:


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


来源:https://stackoverflow.com/questions/28988363/how-to-show-dropdown-only-when-inserting-character-on-multiautocompletetextvie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!