先看效果图
布局文件
<AutoCompleteTextView
android:id="@+id/my_search_auto_edittext"
style="@style/editText_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/x20"
android:layout_toLeftOf="@id/my_search_button"
android:background="@null"
android:completionHint="清除搜索记录"
android:completionThreshold="1"
android:hint="例如:2017年8月 肺炎"
android:completionHintView="@layout/auto_complete_hint_view" android:textCursorDrawable="@drawable/edit_cusor_shape"
</AutoCompleteTextView>
布局中引用到的layout、drawable文件也一并贴出来
auto_complete_hint_view
注意:id设置为Android下的id 而不是R下的id
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorlightGray"
android:gravity="center"
android:onClick="deleteClick"
android:padding="@dimen/x10"
android:textColor="@color/colorPrimary"
android:textSize="15sp">
</TextView>
edit_cusor_shape 光标样式
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="2dp" />
<solid android:color="@color/colorPrimary" />
</shape>
底部“清除搜索记录”的点击事件
public void deleteClick(View view) {
switch (view.getId()) {
case android.R.id.text1:
mList.clear();
//消除提示框
autoTextView.dismissDropDown();
break;
}
}
所涉及到的适配器:
//mList为提示内容的集合
MySearchResultAdapter mAdapter = new MySearchResultAdapter(MySearchActivity.this, mList);
autoTextView.setAdapter(mAdapter);
MySearchResultAdapter .java
public class MySearchResultAdapter extends BaseAdapter implements Filterable {
Context context;
ArrayFilter arrayFilter;//过滤得到的结果类
ArrayList<String> filterList;//结果集合
ArrayList<String> unFilterList;//未过滤集合
public MySearchResultAdapter(Context context, ArrayList<String> filterList) {
this.context = context;
this.filterList = filterList;
}
@Override
public int getCount() {
return filterList.size();
}
@Override
public Object getItem(int position) {
return filterList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = View.inflate(context, R.layout.item_auto_text, null);
viewHolder = new ViewHolder();
viewHolder.tvSearchHistory = (TextView) view.findViewById(R.id.item_auto_text_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tvSearchHistory.setText(filterList.get(position));
return view;
}
@Override
public Filter getFilter() {
if (arrayFilter == null) {
arrayFilter = new ArrayFilter();
}
return arrayFilter;
}
class ViewHolder {
TextView tvSearchHistory;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (unFilterList == null) {
unFilterList = new ArrayList<>();
unFilterList = filterList;
}
if (constraint == null || constraint.length() == 0) {
ArrayList<String> list = unFilterList;
filterResults.values = list;
filterResults.count = list.size();
} else {
String prefixString = constraint.toString().toLowerCase();
ArrayList<String> unFilteredValue = unFilterList;
int count = unFilteredValue.size();
ArrayList<String> newValue = new ArrayList<>();
for (int i = 0; i < count; i++) {
String str = unFilteredValue.get(i);
if (str.startsWith(prefixString)) {
newValue.add(str);
}
}
filterResults.values = newValue;
filterResults.count = newValue.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filterList = (ArrayList<String>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return (CharSequence) resultValue + "FFF";
}
}
}
暂时就是这些内容了,以后涉及到这个内容还会补充进来的。(要多做记录啊,以后)