android小知识之邮箱地址输入自动完成

虽然不难,但是容易忘记,做个备忘吧

package com.guet.zhuge;

  

import android.app.Activity;

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.AutoCompleteTextView;

  

public class MainActivity extends Activity implements TextWatcher{

  

        private AutoCompleteTextView autoview;

        private AutoTextViewAdapter adapter;

          

        private static final String[] AUTO_EMAILS = {"@163.com", "@sina.com", "@qq.com", "@126.com", "@gmail.com", "@apple.com"};

          

        @Override

        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);

                autoview = (AutoCompleteTextView) findViewById(R.id.auto_tv);

                adapter = new AutoTextViewAdapter(this);

                autoview.setAdapter(adapter);

                autoview.setThreshold(1);//输入1个字符时就开始检测,默认为2个

                autoview.addTextChangedListener(this);//监听autoview的变化

        }

  

        @Override

        public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub

                String input = s.toString();  

        adapter.mList.clear();  

        autoAddEmails(input);

        adapter.notifyDataSetChanged();  

        autoview.showDropDown();  

        }

          

        /**

         * 自动填充邮箱列表

         * @param input

         */

        private void autoAddEmails(String input) {

                System.out.println("input-->" + input);

                String autoEmail = "";

        if (input.length() > 0) {  

            for (int i = 0; i < AUTO_EMAILS.length; ++i) {  

                    if(input.contains("@")) {//包含“@”则开始过滤

                            String filter = input.substring(input.indexOf("@") + 1 , input.length());//获取过滤器,即根据输入“@”之后的内容过滤出符合条件的邮箱

                            System.out.println("filter-->" + filter);

                            if(AUTO_EMAILS[i].contains(filter)) {//符合过滤条件

                                    autoEmail = input.substring(0, input.indexOf("@")) + AUTO_EMAILS[i];//用户输入“@”之前的内容加上自动填充的内容即为最后的结果

                                    adapter.mList.add(autoEmail);

                            }

                    }else {

                            autoEmail = input + AUTO_EMAILS[i];

                            adapter.mList.add(autoEmail);

                    }

            }  

        }

        }

  

        @Override

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,

                        int arg3) {

                // TODO Auto-generated method stub

                  

        }

  

        @Override

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                // TODO Auto-generated method stub

                  

        }

}

适配器代码如下:

package com.guet.zhuge;

  

import java.util.ArrayList;

import java.util.List;

  

import android.content.Context;

import android.graphics.Color;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Filter;

import android.widget.Filterable;

import android.widget.TextView;

  

  

public class AutoTextViewAdapter extends BaseAdapter implements Filterable {  

            

        public List<String> mList;  

    private Context mContext;  

    private MyFilter mFilter;  

        

    public AutoTextViewAdapter(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 tv = new TextView(mContext);  

            tv.setTextColor(Color.BLACK);  

            tv.setTextSize(20);  

            convertView = tv;  

        }  

        TextView txt = (TextView) convertView;  

        txt.setText(mList.get(position));  

        return txt;  

    }  

  

    public Filter getFilter() {  

        if (mFilter == null) {  

            mFilter = new MyFilter();  

        }  

        return mFilter;  

    }  

      

    private class MyFilter extends Filter {  

              

            @Override  

            protected FilterResults performFiltering(CharSequence constraint) {  

                    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();  

                    }  

            }  

              

    }  

}

主要的xml 部分文件:

 <AutoCompleteTextView

        android:id="@+id/set_manageaccount_reemail_act_emialaddress"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_marginLeft="10dp"

        android:layout_marginTop="10dp"

        android:layout_marginRight="10dp"

        android:background="@drawable/edt_bg"

        android:singleLine="true"

        android:ems="10"

        android:hint="请输入邮箱地址" >



        <requestFocus />

    </AutoCompleteTextView>

 

 

继续学习!

你可能感兴趣的:(android)