Android:自定义AutoCompleteTextView显示选中后的最后一项
我已经实现了一个自定义的自动完成文本视图,允许用户选择标签;当选择一个项目时,它会正确地调用侦听器来设置视图中选定的文本以及与之对应的颜色。我遇到的问题是,所选项目会立即作为选项之一弹出;我希望项目离开(可能可以通过跟踪被按下的项目来实现,但是有没有更干净的方法来实现这一点或者我遗漏了什么 过滤器:Android:自定义AutoCompleteTextView显示选中后的最后一项,android,android-layout,android-widget,custom-controls,autocompletetextview,Android,Android Layout,Android Widget,Custom Controls,Autocompletetextview,我已经实现了一个自定义的自动完成文本视图,允许用户选择标签;当选择一个项目时,它会正确地调用侦听器来设置视图中选定的文本以及与之对应的颜色。我遇到的问题是,所选项目会立即作为选项之一弹出;我希望项目离开(可能可以通过跟踪被按下的项目来实现,但是有没有更干净的方法来实现这一点或者我遗漏了什么 过滤器: protected class TagFilter extends Filter{ @Override protected FilterResults performFilt
protected class TagFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(resultTags == null){
resultTags = new LinkedList<Tag>();
}
else {
resultTags.clear();
}
if (constraint != null) {
String tagString = constraint.toString();
for (Tag tag : globalTags) {
if (tag.getText().startsWith(tagString)) {
resultTags.add(tag);
}
}
if(resultTags.size() > 1) Collections.sort(resultTags);
}
synchronized (this) {
results.values = resultTags;
results.count = resultTags.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
tagAdapter.clear();
if (results.count > 0) {
for(Tag tagResult : (List<Tag>)results.values){
tagAdapter.add(tagResult);
}
tagAdapter.notifyDataSetChanged();
}
else {
tagAdapter.notifyDataSetInvalidated();
}
}
}
受保护类TagFilter扩展了过滤器{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
if(resultTags==null){
resultTags=newlinkedlist();
}
否则{
结果:clear();
}
if(约束!=null){
String tagString=constraint.toString();
用于(标记:globalTags){
if(tag.getText().startsWith(tagString)){
结果。添加(标记);
}
}
if(resultTags.size()>1)Collections.sort(resultTags);
}
已同步(此){
results.values=结果;
results.count=resultTags.size();
}
返回结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
tagAdapter.clear();
如果(results.count>0){
for(Tag tagResult:(列表)results.values){
tagAdapter.add(tagResult);
}
tagAdapter.notifyDataSetChanged();
}
否则{
tagAdapter.notifyDataSetInvalidated();
}
}
}
标记适配器代码:
protected class TagAdapter extends ArrayAdapter<Tag> implements Filterable {
@Override
public Filter getFilter() {
if(tf == null){
tf = new TagFilter();
}
return tf;
}
public TagAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(CreateActivity.this);
text.setPadding(20, 20, 20, 20);
text.setTextSize(20);
text.setBackgroundColor(Color.BLACK);
text.setTextColor(Color.WHITE);
//TODO holder here
Tag tag = getItem(position);
text.setText(" " + tag.getText(), TextView.BufferType.SPANNABLE);
((Spannable)text.getText()).setSpan(
new BackgroundColorSpan(tag.getColor()),
0,
4,
0);
return text;
}
}
受保护类TagAdapter扩展ArrayAdapter实现可过滤{
@凌驾
公共过滤器getFilter(){
如果(tf==null){
tf=新标记过滤器();
}
返回tf;
}
公共标记适配器(上下文上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
TextView text=新的TextView(CreateActivity.this);
设置填充(20,20,20,20);
text.setTextSize(20);
text.setBackgroundColor(颜色为黑色);
text.setTextColor(Color.WHITE);
//这里是TODO holder
Tag Tag=getItem(位置);
text.setText(“+tag.getText(),TextView.BufferType.SPANNABLE”);
((Spannable)text.getText()).setSpan(
新的BackgroundColorSpan(tag.getColor()),
0,
4.
0);
返回文本;
}
}
AutoCompleteTextView侦听器:
tagText.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Tag selectedTag = resultTags.get((int) arg3);
selectedColor = selectedTag.getColor();
tagText.setText(selectedTag.getText());
tagText.dismissDropDown();
}
});
tagText.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
标记selectedTag=resultTags.get((int)arg3);
selectedColor=selectedTag.getColor();
tagText.setText(selectedTag.getText());
tagText.dismissDropDown();
}
});
有人有主意吗?我最后设置了一个“点击”在onClick侦听器中将标志设置为true,然后当文本被设置并且自动完成尝试再次过滤它时,如果设置了标志并将标志设置为false,我将不返回任何结果,因此正常过滤将继续,直到再次做出选择。我打赌有一种内置的方法可以做到这一点,有人知道吗?