Android自定义AutoCompleteTextView实现自动补全Email

自动补全功能在使用邮箱注册的时候经常使用,这里实现重写AutoCompleteTextView来实现自动补全邮箱功能,代码如下:

 

[java] view plain copy
 
 print?
  1. package com.example.test.autocomplete;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.graphics.Color;  
  8. import android.text.Editable;  
  9. import android.text.TextWatcher;  
  10. import android.util.AttributeSet;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AutoCompleteTextView;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.Filter;  
  16. import android.widget.Filterable;  
  17. import android.widget.TextView;  
  18.   
  19. public class AutoComplete extends AutoCompleteTextView {  
  20.     private static final String[] emailSuffix = { "@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",  
  21.         "@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net",  "@vip.qq.com", "@vip.sina.com" };  
  22.     public AutoComplete(Context context){  
  23.         super(context);  
  24.         init(context);  
  25.     }  
  26.     public AutoComplete(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         init(context);  
  29.     }  
  30.       
  31.     private void init(Context context){  
  32.         final MyAdatper adapter = new MyAdatper(context);  
  33.         setAdapter(adapter);  
  34.         addTextChangedListener(new TextWatcher() {  
  35.             @Override  
  36.             public void afterTextChanged(Editable s) {  
  37.                 String input = s.toString();  
  38.                 adapter.mList.clear();  
  39.                 if (input.length() > 0) {  
  40.                     for (int i = 0; i < emailSuffix.length; ++i) {  
  41.                         adapter.mList.add(input + emailSuffix[i]);  
  42.                     }  
  43.                 }  
  44.                 adapter.notifyDataSetChanged();  
  45.                 showDropDown();  
  46.             }  
  47.   
  48.             @Override  
  49.             public void beforeTextChanged(CharSequence s, int start, int count,  
  50.                     int after) {  
  51.                   
  52.             }  
  53.             @Override  
  54.             public void onTextChanged(CharSequence s, int start, int before,  
  55.                     int count) {  
  56.             }  
  57.         });  
  58.         // default=2 当输入一个字符的时候就开始检测  
  59.         setThreshold(1);  
  60.     }  
  61.       
  62.     class MyAdatper extends BaseAdapter implements Filterable {  
  63.         List<String> mList;  
  64.         private Context mContext;  
  65.         private MyFilter mFilter;  
  66.           
  67.         public MyAdatper(Context context) {  
  68.             mContext = context;  
  69.             mList = new ArrayList<String>();  
  70.         }  
  71.           
  72.         @Override  
  73.         public int getCount() {  
  74.             return mList == null ? 0 : mList.size();  
  75.         }  
  76.   
  77.         @Override  
  78.         public Object getItem(int position) {  
  79.             return mList == null ? null : mList.get(position);  
  80.         }  
  81.   
  82.         @Override  
  83.         public long getItemId(int position) {  
  84.             return position;  
  85.         }  
  86.   
  87.         @Override  
  88.         public View getView(int position, View convertView, ViewGroup parent) {  
  89.             if (convertView == null) {  
  90.                 TextView tv = new TextView(mContext);  
  91.                 tv.setTextColor(Color.BLACK);  
  92.                 tv.setTextSize(20);  
  93.                 convertView = tv;  
  94.             }  
  95.             TextView txt = (TextView) convertView;  
  96.             txt.setText(mList.get(position));  
  97.             return txt;  
  98.         }  
  99.   
  100.         @Override  
  101.         public Filter getFilter() {  
  102.             if (mFilter == null) {  
  103.                 mFilter = new MyFilter();  
  104.             }  
  105.             return mFilter;  
  106.         }  
  107.           
  108.         private class MyFilter extends Filter {  
  109.   
  110.             @Override  
  111.             protected FilterResults performFiltering(CharSequence constraint) {  
  112.                 FilterResults results = new FilterResults();  
  113.                 if (mList == null) {  
  114.                     mList = new ArrayList<String>();  
  115.                 }  
  116.                 results.values = mList;  
  117.                 results.count = mList.size();  
  118.                 return results;  
  119.             }  
  120.   
  121.             @Override  
  122.             protected void publishResults(CharSequence constraint, FilterResults results) {  
  123.                 if (results.count > 0) {  
  124.                     notifyDataSetChanged();  
  125.                 } else {  
  126.                     notifyDataSetInvalidated();  
  127.                 }  
  128.             }  
  129.         }  
  130.     }  
  131. }  

在使用的时候,跟其他组件一样,实现效果如下:

 

posted @ 2016-11-27 20:56  天涯海角路  阅读(252)  评论(0)    收藏  举报