Android SearchView

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

无图无真相

Android SearchView_第1张图片Android SearchView_第2张图片

代码如下:

activity_main.xml



    

    

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



Androidmanifest.xml



    
    

    

    
        
            
                

                
            
            
                
            

            
        
    

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)