您好我想通过工具栏在我的应用中实现搜索功能。目前我在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>
答案 0 :(得分:0)
在菜单文件夹中创建一个菜单项,例如/ 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);
}
然后在您的适配器中实现过滤逻辑,这取决于您搜索的内容以及数据的呈现方式
希望这能指出你正确的方向