在Android AutoCompleteTextView中,当我们正确输入第一个字母时,只显示show dropdown。我想要的是当我在字符串中输入任何字母序列时它应该显示下拉。例如“January”在我的数组中所以当我在AutoComplete字段中输入“anu”,它应该在下拉列表中显示“january”。请帮助。 谢谢
答案 0 :(得分:1)
您可能会编写自己的Filter
并通过TextWatcher
附加。此响应在AutoCompleteTextView
:Android AutoCompleteTextView with Regular Expression?中有一个正则表达式的示例,这是另一个正则表达式/ java示例:How can I perform a partial match with java.util.regex.*?
编辑:您需要扩展ArrayAdapter以覆盖getFilter()并返回自定义过滤器。
所以你会有这样的事情:
autoCompleteTextView.setAdapter(arrayAdapter);
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
arrayAdapter.getFilter().filter(s);
}
});
public class RegexFilter extends Filter{
ArrayAdapter<String> mAdapter;
public RegexFilter(ArrayAdapter<String> adapter) {
mAdapter = adapter;
}
...
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Pattern p = Pattern.compile(constraint);
Matcher m = p.matcher("");
List listOfMatches = new ArrayList<String>();
for (String curMonth : months) {
m.reset(curMonth);
if (m.matches || m.hitEnd()) {
listOfMatches.add(curMonth);
}
}
FilterResults results = new FilterResults();
results.values = listOfMatches;
results.count = listOfMatches.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mAdapter.addAll(results.values);
mAdapter.notifyDataSetChanged();
}
}
public class PartialArrayAdapter extends ArrayAdapter<String> {
...
RegexFilter mFilter;
@Override
public TimedSuggestionFilter getFilter() {
if(null == mFilter)
mFilter = new RegexFilter(this);
return mFilter;
}