0

我有一个MultiAutoCompleteTextView. 它工作正常。但我只想在用户输入 @ 时显示建议下拉列表(比如在 facebook 应用程序中标记用户)。我不知道该怎么做。这是我的代码:

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

3 回答 3

1

创建扩展 MultiAutoCompleteTextView 的自定义文本视图 -> 覆盖 enoughToFilter() -> 将阈值设置为 0(以下给定代码中的粗体变量):

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

}

使用此代码,您将在按下时看到自动建议列表@

于 2016-02-01T18:02:47.403 回答
1

如果您想检测您的字符串以“@”开头(标记)某人或以“#”开头的 hashTag,然后使用它进行查询或过滤,您可以按照以下代码进行操作:

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

它引用了Multiautocompletetextview 链接,仅当用户在“@”键后按下一个键时才显示自动完成下拉列表(如 FB 应用程序中提到的),请向下滚动以查找 @Phuong Sala 答案。

于 2016-09-21T08:10:57.140 回答
0

我自己找到了解决方案。我创建了自定义视图,该视图在其中扩展MultiAutoCompleteTextView和覆盖performFiltering。检查第一个字符是否为“@”,然后过滤其后的下一个字符。否则,将字符替换为“*”以避免过滤。这是我的代码。

@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);
}
于 2015-03-12T06:51:32.163 回答