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

1》效果如下:

执行搜索:

2》布局文件如下:

res/layout/main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/search_et"
        android:hint="@string/search_et_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ListView
        android:id="@+id/content_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>

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<String> 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<String>(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<String>(this,
                    android.R.layout.simple_list_item_1, listItemValues);
            content_lv.setAdapter(listViewAdapter);
        }
        ArrayList<String> tmpList = new ArrayList<String>();
        for (String s : listItemValues) {
            if (s.toLowerCase().contains(searchString.toLowerCase())) {//ListView 数据项中包含搜索字符串(不区分字符大小)
                tmpList.add(s);
            }
        }
            /*
            if (tmpList.size() == 0)
            return;
            */
        listViewAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, tmpList);
        content_lv.setAdapter(listViewAdapter);

        content_lv.invalidateViews();
    }
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页