//menu菜單文件佈局 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:actionViewClass="android.widget.SearchView" android:orderInCategory="1" android:showAsAction="always" android:title=""/> <!-- 如下的這個是本身自定義的SearchView冬天佈局 ,actionLayout填你自定義的佈局名字 --> <item android:id="@+id/action_customer_search" android:actionLayout="@layout/action_view_search" android:icon="@android:drawable/ic_menu_compass" android:orderInCategory="1" android:showAsAction="always|collapseActionView" android:title=""/> </menu> //自定義的SearchView的佈局 <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="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/keyWords" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <Button android:id="@+id/btnId" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="檢索" /> </LinearLayout> //主佈局放一個listview用來加載手機的信息 <ListView android:id="@+id/lvId" android:layout_width="match_parent" android:layout_height="match_parent" /> //ListView中的子佈局 <TextView android:id="@+id/nameId" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:textColor="#00f" android:textSize="25sp" android:textStyle="bold" /> <TextView android:id="@+id/phoneId" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/nameId" android:layout_margin="5dp" android:textColor="#000" android:textSize="20sp" /> //代碼 //加載管理器用getSupportLoaderManager()時要將Activity改成FragmentActivity,不然會報一個錯誤 public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> { private String uri_contacts = "content://com.android.contacts/contacts"; private String uri_raw_contacts = "content://com.android.contacts/raw_contacts"; private String uri_data = "content://com.android.contacts/data"; private String uri_data_phone = "content://com.android.contacts/data/phones"; private String uri_data_email = "content://com.android.contacts/data/emails"; private LoaderManager loaderManager; private SimpleCursorAdapter adapter; private ListView listview; private SearchView searchView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) this.findViewById(R.id.lvId); adapter = new SimpleCursorAdapter(this, R.layout.item_contact_phone, null, new String[] { "display_name", "data1" }, new int[] { R.id.nameId, R.id.phoneId }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listview.setAdapter(adapter); loaderManager = getSupportLoaderManager(); loaderManager.initLoader(1, null, this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // 先填充佈局,再經過佈局拿到控件,在獲得控件的視圖 // 獲取searchView MenuItem searchItem = menu.findItem(R.id.action_search); // 得到控件 searchView = (SearchView) searchItem.getActionView(); 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); loaderManager.restartLoader(1, bundle, MainActivity.this); return false; } }); // 加載自定義的佈局,先獲取獲得佈局,再經過佈局找到所要控件 // 注意這裏找到控件是一個佈局視圖 MenuItem customerSearchView = menu .findItem(R.id.action_customer_search); View view = customerSearchView.getActionView(); final EditText editKey = (EditText) view.findViewById(R.id.keyWords); Button submit = (Button) view.findViewById(R.id.btnId); submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); String keywords = editKey.getText().toString(); bundle.putString("keywords", keywords); loaderManager.restartLoader(1, bundle, MainActivity.this); } }); return super.onCreateOptionsMenu(menu); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { String where = null; String[] args = null; if (bundle != null) { where = "display_name like ? or data1 like ?"; String keywords = "%" + bundle.getString("keywords") + "%"; // 兩個值,若是填入姓名,根據姓名來查,若是填入的是號碼,根據號碼查 args = new String[] { keywords, keywords }; } return new CursorLoader(this, Uri.parse(uri_data_phone), new String[] { "_id", "display_name", "data1" }, where, args, null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.changeCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> arg0) { adapter.swapCursor(null); } }