ActionBar上显示自己自定义的一个SearchView(19)

//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);
	}

}


转载于:https://my.oschina.net/u/2541146/blog/625281

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值