如何在工具栏Android中使用SearchView
我正在使用的代码是使用一个Toolbar
和充气menu
。
这是代码
private Toolbar mToolbar; mToolbar.inflateMenu(R.menu.chat_screen_menu); setupMenu (); private void setupMenu () { mMenu = mToolbar.getMenu(); if (mMenu != null) { if (mChatPager != null && mChatPager.getCurrentItem() > 0) { mMenu.setGroupVisible(R.id.menu_group_chats, true); mMenu.setGroupVisible(R.id.menu_group_contacts, false); } else { mMenu.setGroupVisible(R.id.menu_group_chats, false); mMenu.setGroupVisible(R.id.menu_group_contacts, true); mMenu.setGroupVisible(R.id.menu_group_otr_verified,false); mMenu.setGroupVisible(R.id.menu_group_otr_unverified,false); mMenu.setGroupVisible(R.id.menu_group_otr_off,false); } } mToolbar.setOnMenuItemClickListener(new OnMenuItemClickListener () { .......... } }
但现在,他们需要在tool_bar
的Search
button。 我设法把它,我在这里跟随一个指导当我试图写一些东西来search,我已经把testinglistener
从来没有显示的吐司。 这表明listener
器不工作
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.chat_screen_menu, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_menu_search)); final Toast toast = new Toast(mApp); if (mSearchView != null ) { mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); mSearchView.setIconifiedByDefault(false); SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { public boolean onQueryTextChange(String newText) { mSearchString = newText; //doFilterAsync(mSearchString); toast.makeText(getApplicationContext(), "Test1", Toast.LENGTH_LONG).show(); return true; } public boolean onQueryTextSubmit(String query) { mSearchString = query; //doFilterAsync(mSearchString); toast.makeText(getApplicationContext(), "Test2", Toast.LENGTH_LONG).show(); return true; } }; mSearchView.setOnQueryTextListener(queryTextListener); } return true; }
你必须使用Appcompat库。 用法如下:
dashboard.xml
<item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always" app:actionViewClass="android.support.v7.widget.SearchView" android:title="Search"/>
活动文件:
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.dashboard, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE); SearchView searchView = null; if (searchItem != null) { searchView = (SearchView) searchItem.getActionView(); } if (searchView != null) { searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName())); } return super.onCreateOptionsMenu(menu); }
清单文件:
<meta-data android:name="android.app.default_searchable" android:value="com.apkgetter.SearchResultsActivity" /> <activity android:name="com.apkgetter.SearchResultsActivity" android:label="@string/app_name" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity>
可search的xml文件:
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_name" />
最后,您的SearchResultsActivity类代码。 显示你的search结果。
如果您想在Fragment
设置searchfunction,只需添加以下几行:
第1步 – 添加search字段到你的toolbar
:
<item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" android:title="Search"/>
第2步 – 将逻辑添加到您的onCreateOptionsMenu()
import android.support.v7.widget.SearchView; // not the default ! @Override public boolean onCreateOptionsMenu( Menu menu) { getMenuInflater().inflate( R.menu.main, menu); MenuItem myActionMenuItem = menu.findItem( R.id.action_search); searchView = (SearchView) myActionMenuItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { // Toast like print UserFeedback.show( "SearchOnQueryTextSubmit: " + query); if( ! searchView.isIconified()) { searchView.setIconified(true); } myActionMenuItem.collapseActionView(); return false; } @Override public boolean onQueryTextChange(String s) { // UserFeedback.show( "SearchOnQueryTextChanged: " + s); return false; } }); return true; }
如果你想直接在工具栏中添加它。
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content"> <SearchView android:id="@+id/searchView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="Search" android:layout_centerHorizontal="true" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout>