[英]Implement searchview in toolbar in android
嗨,我想在我的應用程序中通過工具欄實現搜索功能。目前,我已經在activity_main.xml中添加了SearchView元素,但我不知道如何實現剩余的邏輯。 請指導我解決這個問題
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CC33"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/app_name">
<SearchView
android:id="@+id/mySearchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="true"
android:layout_gravity="end"
android:queryHint="Search by name"/>
</android.support.v7.widget.Toolbar>
我的AndroidManifest.xml是
<application
android:allowBackup="true"
android:icon="@drawable/splash_img"
android:label="Call Logger"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.comforters.callLogger.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.comforters.callLogger.MainActivity"
android:label="Call Logger" >
</activity>
<receiver android:name=".PhonecallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
在菜單文件夾中創建一個菜單項,說/ main / res / menu具有以下內容
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</item>
該菜單應綁定到將處理搜索的活動。 在您的活動中輸入以下內容
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_activity_main, menu);
super.onCreateOptionsMenu(menu, inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(context.getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String query)
{
adapter.getFilter().filter(query);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
然后在您的適配器中實現過濾邏輯,這取決於您要搜索的內容以及數據的顯示方式
希望這能為您指明正確的方向
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.