AutoCompleteTextView自动填充邮箱后缀

来源:互联网 发布:plc三种编程语言 编辑:程序博客网 时间:2024/05/01 13:57

main.xml如下:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <AutoCompleteTextView  
  8.         android:id="@+id/edit"  
  9.         android:layout_width="230dip"  
  10.         android:layout_height="wrap_content"  
  11.         android:textSize="20sp"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:layout_marginTop="20dip" />  
  14.   
  15. </LinearLayout>  

程序代码如下:

[java] view plaincopyprint?
  1. package com.android.system.AutoComplete;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.graphics.Color;  
  9. import android.os.Bundle;  
  10. import android.text.Editable;  
  11. import android.text.TextWatcher;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AutoCompleteTextView;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.Filter;  
  18. import android.widget.Filterable;  
  19. import android.widget.TextView;  
  20.   
  21. public class AutoComplete extends Activity {  
  22.     AutoCompleteTextView autoview;  
  23.   
  24.     // default city  
  25.     String[] stringArray = { "@163.com""@126.com""@qq.com""@sina.com""@taobao.com" };  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.   
  32.         autoview = (AutoCompleteTextView) findViewById(R.id.edit);  
  33. //      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, stringArray);  
  34.         final MyAdatper adapter = new MyAdatper(this);  
  35.         autoview.setAdapter(adapter);  
  36.         autoview.addTextChangedListener(new TextWatcher() {  
  37.             @Override  
  38.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  39.             }  
  40.             @Override  
  41.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  42.             }  
  43.             @Override  
  44.             public void afterTextChanged(Editable s) {  
  45.                 String input = s.toString();  
  46.                 adapter.mList.clear();  
  47.                 if (input.length() > 0) {  
  48.                     for (int i = 0; i < stringArray.length; ++i) {  
  49.                         adapter.mList.add(input + stringArray[i]);  
  50.                     }  
  51.                 }  
  52.                 adapter.notifyDataSetChanged();  
  53.                 autoview.showDropDown();  
  54.                   
  55.             }  
  56.         });  
  57.   
  58.         // default=2  
  59.         autoview.setThreshold(1);  
  60.     }  
  61.       
  62.     class MyAdatper extends BaseAdapter implements Filterable {  
  63.   
  64.         List<String> mList;  
  65.         private Context mContext;  
  66.         private MyFilter mFilter;  
  67.           
  68.         public MyAdatper(Context context) {  
  69.             mContext = context;  
  70.             mList = new ArrayList<String>();  
  71.         }  
  72.           
  73.         @Override  
  74.         public int getCount() {  
  75.             return mList == null ? 0 : mList.size();  
  76.         }  
  77.   
  78.         @Override  
  79.         public Object getItem(int position) {  
  80.             return mList == null ? null : mList.get(position);  
  81.         }  
  82.   
  83.         @Override  
  84.         public long getItemId(int position) {  
  85.             return position;  
  86.         }  
  87.   
  88.         @Override  
  89.         public View getView(int position, View convertView, ViewGroup parent) {  
  90.             if (convertView == null) {  
  91.                 TextView tv = new TextView(mContext);  
  92.                 tv.setTextColor(Color.BLACK);  
  93.                 tv.setTextSize(20);  
  94.                 convertView = tv;  
  95.             }  
  96.             TextView txt = (TextView) convertView;  
  97.             txt.setText(mList.get(position));  
  98.             return txt;  
  99.         }  
  100.   
  101.         @Override  
  102.         public Filter getFilter() {  
  103.             if (mFilter == null) {  
  104.                 mFilter = new MyFilter();  
  105.             }  
  106.             return mFilter;  
  107.         }  
  108.           
  109.         private class MyFilter extends Filter {  
  110.   
  111.             @Override  
  112.             protected FilterResults performFiltering(CharSequence constraint) {  
  113.                 FilterResults results = new FilterResults();  
  114.                 if (mList == null) {  
  115.                     mList = new ArrayList<String>();  
  116.                 }  
  117.                 results.values = mList;  
  118.                 results.count = mList.size();  
  119.                 return results;  
  120.             }  
  121.   
  122.             @Override  
  123.             protected void publishResults(CharSequence constraint, FilterResults results) {  
  124.                 if (results.count > 0) {  
  125.                     notifyDataSetChanged();  
  126.                 } else {  
  127.                     notifyDataSetInvalidated();  
  128.                 }  
  129.             }  
  130.               
  131.         }  
  132.           
  133.     }  
  134.       
  135. }  

代码下载地址:http://download.csdn.net/detail/leiming32/4469341
0 0
原创粉丝点击