功能:查找通讯录 -- 输入关键字进行检索
效果图示例:
1、现在清单配置里添加查找联系人的 权限 READ_CONTACTS
2、在res下创建一个menu菜单文件夹
菜单文件夹中菜单的布局main_menu.xml -- 2种SearchView的创建方式
代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 方式1:actionViewClass -->
<item
android:id="@+id/seachView"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always"
android:title=""/>
<!-- 方式2:actionLayout
加 | 表示 有多个属性 collapseActionView 表示折叠起来该视图 -->
<item
android:id="@+id/custom_seachView"
android:actionLayout="@layout/item_activity"
android:showAsAction="always|collapseActionView"
android:icon=" :drawable/ic_menu_compass"
android:title=""/>
</menu>
==============
3、res/layout界面有2个布局一个主布局(ListView显示电话和联系人)一个item布局(SearchView自定义布局)
activity_main.xml 布局
代码
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
-----------------------
item_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="horizontal" >
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:id="@+id/bt_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="检索"/>
</LinearLayout>
==========================
4、MainActivity类
代码
//实现LoaderCallbacks接口
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
private ListView listView;
private SearchView searchView;
private SimpleCursorAdapter adapter;//游标适配器
private LoaderManager loaderManager;
//联系人通讯录路径
private String url_phone = "content://com.android.contacts/data/phones";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listView = (ListView) this.findViewById(R.id.listview);
//this.editId = (EditText) this.findViewById(R.id.edit_text);
//只检索通讯录的名字和电话
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
new String[] { "display_name", "data1" }, new int[] {
android.R.id.text1,android.R.id.text2},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
loaderManager = getSupportLoaderManager();
loaderManager.initLoader(1, null, this);
}
//======================LoaderCallbacks接口要重写的方法=======================
@Override//创建加载器
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
if (bundle != null) {
String keywords = bundle.getString("keywords");
return new CursorLoader(this, Uri.parse(url_phone), new String[] {
"_id","display_name", "data1" },
"display_name like ? or data1 like ?", new String[] {
keywords + "%", keywords + "%" }, null);
}
return new CursorLoader(this, Uri.parse(url_phone), new String[] {
"_id","display_name", "data1" }, null, null, null);
}
@Override//加载器创建完成
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
//选择菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.seachView);//Android自带检索
searchView = (SearchView) item.getActionView();
//通过setOnQueryTextListener方法来监听
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String keywords) {
Bundle bundle = new Bundle();
bundle.putString("keywords", keywords);//用户输入要检索的内容为keywords
//从新查找一遍 -- 调用onCreateLoader方法
//参数1 -- 加载器标记(有该标记加载器就不创建 否则就创建)
//参数2 -- Bundle
loaderManager.restartLoader(1, bundle, MainActivity.this);
return false;
}
});
//自定义自动检索
View custom_searchView = menu.findItem(R.id.custom_seachView).getActionView();
final EditText editId = (EditText) custom_searchView.findViewById(R.id.edit_text);
custom_searchView.findViewById(R.id.bt_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String edit_contact = editId.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("keywords", edit_contact);
loaderManager.restartLoader(1, bundle, MainActivity.this);
}
});
return super.onCreateOptionsMenu(menu);
}
}