MainActivity如下:
package cc.testautocompletetextview2; import android.os.Bundle; import android.app.Activity; /** * Demo描述 * 利用自定义AutoCompleteTextView完成邮箱自动补全功能 * * 参考资料: * 1 http://blog.csdn.net/fx_sky/article/details/9326129 * 2 http://blog.csdn.net/jwzhangjie/article/details/15771953 * 3 http://my.eoe.cn/eoe_haozi/archive/519.html * 4 http://gundumw100.iteye.com/blog/1056728 * Thank you very much */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
AutoCompleteTextViewSubClass如下:
package cc.testautocompletetextview2; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Color; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.AutoCompleteTextView; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; public class AutoCompleteTextViewSubClass extends AutoCompleteTextView { //可补全的邮箱后缀 private static final String[] emailSuffix = { "@qq.com","@gmail.com", "@sina.com","@sohu.com"}; public AutoCompleteTextViewSubClass(Context context){ super(context); init(context); } public AutoCompleteTextViewSubClass(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context){ final AutoCompleteTextViewAdatper adapter = new AutoCompleteTextViewAdatper(context); setAdapter(adapter); //设置阈值 //等同于布局文件中的 android:completionThreshold="2" //setThreshold(2); addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable editable) { String inputString = editable.toString(); System.out.println("--->输入的内容 inputString="+inputString); adapter.mList.clear(); if (inputString.length() > 0) { for (int i = 0; i < emailSuffix.length; ++i) { adapter.mList.add(inputString + emailSuffix[i]); } } adapter.notifyDataSetChanged(); showDropDown(); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { } @Override public void onTextChanged(CharSequence s, int start, int before,int count) { } }); } //定义AutoCompleteTextView的Adapter继承自BaseAdapter且实现Filterable接口 class AutoCompleteTextViewAdatper extends BaseAdapter implements Filterable { List<String> mList; private Context mContext; private FilterSubClass mFilter; public AutoCompleteTextViewAdatper(Context context) { mContext = context; mList = new ArrayList<String>(); } @Override public int getCount() { return mList == null ? 0 : mList.size(); } @Override public Object getItem(int position) { return mList == null ? null : mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { TextView textView = new TextView(mContext); textView.setTextColor(Color.RED); textView.setTextSize(30); convertView = textView; } TextView textView = (TextView) convertView; textView.setText(mList.get(position)); return textView; } @Override public Filter getFilter() { if (mFilter == null) { mFilter = new FilterSubClass(); } return mFilter; } // 只有满足setThreshold(2)这个阈值时才会执行performFiltering() private class FilterSubClass extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { System.out.println("--->过滤 constraint=" + constraint.toString()); FilterResults results = new FilterResults(); if (mList == null) { mList = new ArrayList<String>(); } results.values = mList; results.count = mList.size(); return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义AutoCompleteTextView使用示例" android:layout_centerHorizontal="true"/> <cc.testautocompletetextview2.AutoCompleteTextViewSubClass android:id="@+id/autoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dip" android:completionHint="请选择:" android:dropDownVerticalOffset="2dip" android:dropDownHorizontalOffset="2dip" android:completionThreshold="2" /> </RelativeLayout>