Android SearchView

项目需要实现Search功能,根据网上的一些文章和Android的文档,自己实现了一个搜索联系人的。测试手机为三星的

无图无真相

Android SearchView_第1张图片

代码如下:

activity_main.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"
    tools:context=".MainActivity" >

    <SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F0F0F0F0" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

</LinearLayout>
searchable.xml,这个文件至关重要,可以配置搜索模式,比如voicesearch,用于链接到设备的全局搜索。如果不希望加到全局搜索,可以不配置这个文件。

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/contactsList"
    android:hint="@string/searchHint"
    android:searchMode="queryRewriteFromText" />

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.contactsearch"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.contactsearch.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>
MainActivity.java

package com.demo.contactsearch;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.RawContacts;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

	private SearchView mSearchView;
	private ListView mListView;
	private SimpleCursorAdapter mAdapter;
	private Cursor mCursor;
	
	static final String[] PROJECTION = new String[] {
		ContactsContract.RawContacts._ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY };

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

		mCursor = getContentResolver().query(RawContacts.CONTENT_URI, PROJECTION, null, null, null);
		mAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_1, mCursor,
				new String[] { RawContacts.DISPLAY_NAME_PRIMARY }, new int[] { android.R.id.text1 },
				0);
		mListView = (ListView) findViewById(android.R.id.list);
		mListView.setAdapter(mAdapter);
		
		mSearchView = (SearchView) findViewById(R.id.search);
		mSearchView.setIconifiedByDefault(true);
		mSearchView.onActionViewExpanded();
		mSearchView.setFocusable(false);
		mSearchView.clearFocus();
//		mSearchView.setIconifiedByDefault(true);
		mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextChange(String queryText) {
				String selection = RawContacts.DISPLAY_NAME_PRIMARY + " LIKE '%"
						+ queryText + "%' " + " OR "
						+ RawContacts.SORT_KEY_PRIMARY + " LIKE '%" + queryText
						+ "%' ";
//				String[] selectionArg = { queryText };
				mCursor = getContentResolver().query(RawContacts.CONTENT_URI, PROJECTION, selection, null, null);
				mAdapter.swapCursor(mCursor);
				return true;
			}

			@Override
			public boolean onQueryTextSubmit(String queryText) {
				if (mSearchView != null) {
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					if (imm != null) {
						imm.hideSoftInputFromWindow(
								mSearchView.getWindowToken(), 0);
					}
					mSearchView.clearFocus();
				}
				return true;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



你可能感兴趣的:(Android SearchView)