ActionBar上显示自己自定义的一个SearchView(19)
2016-02-27 00:00
381 查看
摘要: 在ActionBar上显示自己自定义视图,首先你要建一个布局文件,在里面放什么控件,然后再在res/menu/文件夹下建一个菜单文件,然后菜单文件引用你自定义的布局即可
//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); } }
相关文章推荐
- ViewPager的一个简单示例,这个比较乱(20)
- 第10课:底实战详解使用Java开发Spark程序学习笔记
- spring【7】(spring事务详解)
- 算法:回溯算法之九宫格
- WINDOWS API HOOK
- js keypress 对输入中文不响应
- 本地远程连接linux服务器的mysql数据库
- redmine数据备份
- redmine版本控制svn
- Netty精粹之TCP粘包拆包问题
- OC高效率52之理解NSCopying协议
- NIO总结
- Ubuntu Server Deploy 已经支持php7
- SSH协议介绍
- CentOS-6.5系统基础优化附带优化脚本
- centos ab使用
- ios CGPoint CGFloat CGSize 的运用
- ios 跳转的运用 push和modal
- ios 代码创建UIToolBar
- 程序开发中那些莫名奇妙的bug