Android中控件AutoCompleteTextView的使用方法和一些属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <AutoCompleteTextView android:id="@+id/auto_seekTips" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:completionThreshold="1" android:dropDownWidth="match_parent" android:dropDownVerticalOffset="10dp" android:hint="请输入" android:inputType="text" android:lines="1" android:maxLength="20" android:paddingLeft="15dp" android:textSize="15sp" /> </LinearLayout>
package com.chuanye.mybdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.AdapterView; import android.widget.AutoCompleteTextView; import java.util.ArrayList; import java.util.List; public class Main5Activity extends AppCompatActivity { //搜索提示控件 private AutoCompleteTextView auto_seektips; private List<Result> resultsList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main5); //将控件实例化 auto_seektips = findViewById(R.id.auto_seekTips); Result result1 = new Result("aaa","aaaa"); Result result2 = new Result("bbb","bbbb"); Result result3 = new Result("ccc","cccc"); Result result4 = new Result("ddd","dddd"); Result result5 = new Result("eee","eeee"); Result result6 = new Result("fff","ffff"); resultsList.add(result1); resultsList.add(result2); resultsList.add(result3); resultsList.add(result4); resultsList.add(result5); resultsList.add(result6); Result result7 = new Result("aaa1","aaaaa"); Result result8 = new Result("bbb1","bbbbb"); Result result9 = new Result("ccc1","ccccc"); Result result10 = new Result("ddd1","ddddd"); Result result11 = new Result("eee1","eeeee"); Result result12 = new Result("fff1","fffff"); resultsList.add(result7); resultsList.add(result8); resultsList.add(result9); resultsList.add(result10); resultsList.add(result11); resultsList.add(result12); //自定义适配器的设置 AutoCompleteTextAdapter adapter=new AutoCompleteTextAdapter(resultsList, Main5Activity.this); auto_seektips.setAdapter(adapter); //AutoCompleteAdapter文本内容变化前 ,变化时,变化后的一个实时监听 //AutoCompleteTextView控件的点击事件 auto_seektips.addTextChangedListener(new TextWatcher() { @Override//改变之前 public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override//改变时 public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override//改变后 public void afterTextChanged(Editable s) { } }); //AutoCompleteAdapter点击事件的设置, auto_seektips.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { auto_seektips.setText(""); //获取到过滤后的结果的集合 ArrayList<Result> results=AutoCompleteTextAdapter.newvalues; if (results != null && results.size() > 0){ Result result=results.get(position); String s=result.getDistrict(); auto_seektips.setText(s); //将光标放到文本最后 auto_seektips.setSelection(auto_seektips.getText().length()); }else { auto_seektips.setText(""); } } }); } }
package com.chuanye.mybdemo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class AutoCompleteTextAdapter extends BaseAdapter implements Filterable { private ArrayFilter mFilter;//数据过滤器 private List<Result> mList;//传进来的数据 private ArrayList<Result> mFilteredData;// private Context context;//上下文 public static ArrayList<Result> newvalues; public AutoCompleteTextAdapter(List<Result> mList, Context context) { this.mList = mList; this.context = context; } @Override public int getCount() { return mList == null ? 0 : mList.size(); } @Override public Object getItem(int position) { return mList.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.seektipitem2, null); viewHolder = new ViewHolder(); viewHolder.tv_city = view.findViewById(R.id.tv_city); viewHolder.tv_city1 = view.findViewById(R.id.tv_city1); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } Result resultlist = mList.get(position); viewHolder.tv_city.setText(resultlist.getName()); viewHolder.tv_city1.setText(resultlist.getDistrict()); return view; } class ViewHolder { public TextView tv_city; public TextView tv_city1; } /** * 在后台线程执行,定义过滤算法 * getFilter()方法会返回一个Filter对象,Filter是一个数据过滤器,其过滤操作是通过performFiltering()方法和publishResult()方法完成的。 * performFiltering()方法进行过滤操作,publishResult()方法用于发表过滤操作结果。 * * @return */ @Override public Filter getFilter() { //创建过滤器的对象 if (mFilter == null) { mFilter = new ArrayFilter(); } return mFilter; } //过滤器类 class ArrayFilter extends Filter { /** * @param constraint:用户在输入框中所输入的内容 * @return */ @Override//进行过滤操作 protected FilterResults performFiltering(CharSequence constraint) { //使用过滤操作的结果 FilterResults results = new FilterResults(); if (mFilteredData == null) { //以一个集合初始化mFilteredData=new ArrayList<String>(mList);//mList(数组容量)为集合 mFilteredData = new ArrayList<>(mList); } //如果没有输入内容,则不过滤 if (constraint == null || constraint.length() == 0) { ArrayList<Result> list = mFilteredData; results.values = list; results.count = list.size(); } else { //过滤的条件 String constraintString = constraint.toString().toLowerCase(); //将传进来的全部数据赋值给filteredValues ArrayList<Result> filteredValues = mFilteredData; int count = filteredValues.size(); newvalues = new ArrayList<>(count); //循环变量数据源,如果有属性满足过滤条件,则添加到result中 for (int i = 0; i < count; i++) { Result resultData = filteredValues.get(i); if (resultData != null) { //过滤条件 if (resultData.getDistrict() != null && resultData.getDistrict().startsWith(constraintString)) { newvalues.add(resultData); } } results.values = newvalues; results.count = newvalues.size(); } } return results; } @Override//发表过滤操作结果 protected void publishResults(CharSequence constraint, FilterResults results) { mList = (List<Result>) results.values; if (results.count > 0) { //重绘当前可见区域 notifyDataSetChanged(); } else { //重绘控件,还原到初始状态 notifyDataSetInvalidated(); } } } }
来源:https://blog.csdn.net/wpn_931/article/details/82799930