Android学习系列之控件 AutoCompleteTextView邮箱后缀自动补全

来源:互联网 发布:店铺淘宝客有风险吗 编辑:程序博客网 时间:2024/05/01 06:36

图:

由于原有的AutoCompleteTextView只是按照相同的字符串匹配,所以这里要自定义AutoCompleteTextView,然后复写里面的一些方法

[java] view plain copy
 print?
  1. public class EmailAutoCompleteTextView extends AutoCompleteTextView {  
  2.   
  3.     private static final String TAG = "EmailAutoCompleteTextView";  
  4.   
  5.     private String[] emailSufixs = new String[]{"@qq.com""@163.com""@126.com""@gmail.com""@sina.com""@hotmail.com",  
  6.             "@yahoo.cn""@sohu.com""@foxmail.com""@139.com""@yeah.net""@vip.qq.com""@vip.sina.com"};  
  7.   
  8.     public EmailAutoCompleteTextView(Context context) {  
  9.         super(context);  
  10.         init(context);  
  11.     }  
  12.   
  13.   
  14.     public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         init(context);  
  17.     }  
  18.   
  19.   
  20.     public EmailAutoCompleteTextView(Context context, AttributeSet attrs,  
  21.                                      int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         init(context);  
  24.     }  
  25.   
  26.   
  27.     public void setAdapterString(String[] es) {  
  28.         if (es != null && es.length > 0)  
  29.             this.emailSufixs = es;  
  30.     }  
  31.   
  32.   
  33.     private void init(final Context context) {  
  34.         //adapter中使用默认的emailSufixs中的数据,可以通过setAdapterString来更改  
  35.         this.setAdapter(new EmailAutoCompleteAdapter(context, R.layout.register_auto_complete_item, emailSufixs));  
  36.         //使得在输入1个字符之后便开启自动完成  
  37.         this.setThreshold(1);  
  38.         this.setOnFocusChangeListener(new OnFocusChangeListener() {  
  39.             @Override  
  40.             public void onFocusChange(View v, boolean hasFocus) {  
  41.                 if (hasFocus) {  
  42.                     String text = EmailAutoCompleteTextView.this.getText().toString();  
  43.                     //当该文本域重新获得焦点后,重启自动完成  
  44.                     if (!"".equals(text))  
  45.                         performFiltering(text, 0);  
  46.                 } else {  
  47.                     //当文本域丢失焦点后,检查输入email地址的格式  
  48.                     EmailAutoCompleteTextView ev = (EmailAutoCompleteTextView) v;  
  49.                     String text = ev.getText().toString();  
  50.                     //这里正则写的有点粗暴:)  
  51.                     if (text != null && text.matches("^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$")) {  
  52.   
  53.                     } else {  
  54.                         Toast toast = Toast.makeText(context, "邮件地址格式不正确", Toast.LENGTH_SHORT);  
  55.                         toast.show();  
  56.                     }  
  57.                 }  
  58.             }  
  59.         });  
  60.     }  
  61.   
  62.     @Override  
  63.     protected void replaceText(CharSequence text) {  
  64.         //当我们在下拉框中选择一项时,android会默认使用AutoCompleteTextView中Adapter里的文本来填充文本域  
  65.         //因为这里Adapter中只是存了常用email的后缀  
  66.         //因此要重新replace逻辑,将用户输入的部分与后缀合并  
  67.         Log.i(TAG + " replaceText", text.toString());  
  68.         String t = this.getText().toString();  
  69.         int index = t.indexOf("@");  
  70.         if (index != -1)  
  71.             t = t.substring(0, index);  
  72.         super.replaceText(t + text);  
  73.     }  
  74.   
  75.   
  76.     @Override  
  77.     protected void performFiltering(CharSequence text, int keyCode) {  
  78.         //该方法会在用户输入文本之后调用,将已输入的文本与adapter中的数据对比,若它匹配  
  79.         //adapter中数据的前半部分,那么adapter中的这条数据将会在下拉框中出现  
  80.         Log.i(TAG + " performFiltering", text.toString() + "   " + keyCode);  
  81.         String t = text.toString();  
  82.         //因为用户输入邮箱时,都是以字母,数字开始,而我们的adapter中只会提供以类似于"@163.com"  
  83.         //的邮箱后缀,因此在调用super.performFiltering时,传入的一定是以"@"开头的字符串  
  84.         int index = t.indexOf("@");  
  85.         if (index == -1) {  
  86.             if (t.matches("^[a-zA-Z0-9_]+$")) {  
  87.                 super.performFiltering("@", keyCode);  
  88.             } else  
  89.                 this.dismissDropDown();//当用户中途输入非法字符时,关闭下拉提示框  
  90.         } else {  
  91.             super.performFiltering(t.substring(index), keyCode);  
  92.         }  
  93.     }  
  94.   
  95.   
  96.     private class EmailAutoCompleteAdapter extends ArrayAdapter<String> {  
  97.         public EmailAutoCompleteAdapter(Context context, int textViewResourceId, String[] email_s) {  
  98.             super(context, textViewResourceId, email_s);  
  99.         }  
  100.   
  101.         @Override  
  102.         public View getView(int position, View convertView, ViewGroup parent) {  
  103.             Log.i(TAG, "in GetView");  
  104.             View v = convertView;  
  105.             if (v == null)  
  106.                 v = LayoutInflater.from(getContext()).inflate(  
  107.                         R.layout.register_auto_complete_item, null);  
  108.             TextView tv = (TextView) v.findViewById(R.id.tv);  
  109.             String t = EmailAutoCompleteTextView.this.getText().toString();  
  110.             int index = t.indexOf("@");  
  111.             if (index != -1)  
  112.                 t = t.substring(0, index);  
  113.             //将用户输入的文本与adapter中的email后缀拼接后,在下拉框中显示  
  114.             tv.setText(t + getItem(position));  
  115.             Log.i(TAG, tv.getText().toString());  
  116.             return v;  
  117.         }  
  118.   
  119.     }  
  120.   
  121. }  

下拉框的item的xml文件

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  4.           android:id="@+id/tv"  
  5.           android:padding="8dp"  
  6.           android:layout_width="match_parent"  
  7.           android:layout_height="wrap_content"/>  

现在就可以直接引用了,引用的时候注意自定义文件的路径

[html] view plain copy
 print?
  1. <com.example.view.widgets.EmailAutoCompleteTextView  
  2.             android:id="@+id/act"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="wrap_content"  
  5.             android:hint="请输入您常用的邮箱"  
  6.             android:textColor="@color/black"  
  7.             android:shadowColor="@color/gray"  
  8.             android:shadowRadius="1"  
  9.             android:numeric="decimal"/>  

到这里,自定义补全的功能全部实现了,但是有一点是样式的问题,如下图:


系统默认的EditText的边框颜色是橘黄色,但是想改就得自己手动修改了

修改的方法其实很简单,只要在xml文件中应用该自定义框的地方设置background的属性就好了

0 0