实现类似Android联系人搜索功能

1》效果如下:

实现类似Android联系人搜索功能_第1张图片

执行搜索:

实现类似Android联系人搜索功能_第2张图片

2》布局文件如下:

res/layout/main_activity.xml




    
    

    



3》MainActivity.java

package com.sohu.cxc.edittextsearchdemo;

import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.logging.Handler;


public class MainActivity extends ActionBarActivity {
    private static final String TAG = "MainActivity";
    //声明控件
    private EditText search_et;
    private ListView content_lv;

    //ListView 中要填充的数据
    private String[] listItemValues = {"Java", "JavaSE", "JavaEE", "Oracle 8",
            "Oracle 9i", "Oracle 10g"};

    private ArrayAdapter listViewAdapter;

    private android.os.Handler myHandler;
    private static final int MSG_SINGAL = 1001;
    private static final String MSG_KEY = "MainActivity.MSG_KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    //初始化控件、变量并对注册相应的事件
    private void initViews() {
        search_et = (EditText) findViewById(R.id.search_et);
        content_lv = (ListView) findViewById(R.id.content_lv);

        //注册事件
        search_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                Message msg = new Message();
                msg.what = MSG_SINGAL;
                Bundle data = new Bundle();
                data.putString(MSG_KEY, s.toString());
                msg.setData(data);
                myHandler.sendMessage(msg);
                Log.d(TAG, "----send:" + s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        listViewAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItemValues);
        //注册Adapter
        content_lv.setAdapter(listViewAdapter);

        //handler

        myHandler = new android.os.Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_SINGAL:
                        //todo
                        refreshListView(msg.getData().getString(MSG_KEY));
                        break;
                    default:
                        break;
                }

            }
        };

    }

    //根据搜索字符(串)更新ListView显示数据
    private void refreshListView(String searchString) {
        Log.d(TAG, "+++++++get:" + searchString);
        if (searchString == null || searchString.trim().length() == 0) {//搜索字符串为空时,显示全部
            listViewAdapter = new ArrayAdapter(this,
                    android.R.layout.simple_list_item_1, listItemValues);
            content_lv.setAdapter(listViewAdapter);
        }
        ArrayList tmpList = new ArrayList();
        for (String s : listItemValues) {
            if (s.toLowerCase().contains(searchString.toLowerCase())) {//ListView 数据项中包含搜索字符串(不区分字符大小)
                tmpList.add(s);
            }
        }
            /*
            if (tmpList.size() == 0)
            return;
            */
        listViewAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, tmpList);
        content_lv.setAdapter(listViewAdapter);

        content_lv.invalidateViews();
    }
}



你可能感兴趣的:(Android)