android-Adding Search Functionality,Setting Up the Search Interface
2016-01-31 17:38
591 查看
Android's built-in search features offer apps an easy way to provide a consistent search experience for all users. There are two ways to implement search in your app depending
on the version of Android that is running on the device. This class covers how to add search with
which was introduced in Android 3.0, while maintaining backward compatibility with older versions of Android by using the default search dialog provided by the system.
Beginning in Android 3.0, using the
the app bar is the preferred way to provide search in your app. Like with all items in the app bar, you can define the
show at all times, only when there is room, or as a collapsible action, which displays the
an icon initially, then takes up the entire app bar as a search field when the user clicks the icon.
Note: Later in this class, you will learn how to make your app compatible down to Android 2.1 (API level 7) for devices that do not support
Because of the limited app bar space on handset devices, using the
Note: If you already have an existing XML file for your menu items, you can add the
> In the
that you created before, associate the searchable configuration with the
calling
In your searchable activity, handle the
by checking for it in your
Note: If your searchable activity launches in single top mode (
in the
top mode, only one instance of your activity is created and subsequent calls to start your activity do not create a new activity on the stack. This launch mode is useful so users can perform searches from the same activity without creating a new activity instance
every time.
on the version of Android that is running on the device. This class covers how to add search with
SearchView,
which was introduced in Android 3.0, while maintaining backward compatibility with older versions of Android by using the default search dialog provided by the system.
Beginning in Android 3.0, using the
SearchViewwidget as an item in
the app bar is the preferred way to provide search in your app. Like with all items in the app bar, you can define the
SearchViewto
show at all times, only when there is room, or as a collapsible action, which displays the
SearchViewas
an icon initially, then takes up the entire app bar as a search field when the user clicks the icon.
Note: Later in this class, you will learn how to make your app compatible down to Android 2.1 (API level 7) for devices that do not support
SearchView.
Because of the limited app bar space on handset devices, using the
collapsibleActionViewattribute is recommended to provide a better user experience.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:title="@string/search_title" android:icon="@drawable/ic_search" android:showAsAction="collapseActionView|ifRoom" android:actionViewClass="android.widget.SearchView" /> </menu>
Note: If you already have an existing XML file for your menu items, you can add the
<item>element to that file instead.
> In the
onCreateOptionsMenu()method
that you created before, associate the searchable configuration with the
SearchViewby
calling
setSearchableInfo(SearchableInfo):
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); // Associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return true; }
<activity android:name=".SearchResultsActivity" ... > ... <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> ... </activity>
In your searchable activity, handle the
ACTION_SEARCHintent
by checking for it in your
onCreate()method.
Note: If your searchable activity launches in single top mode (
android:launchMode="singleTop"), also handle the
ACTION_SEARCHintent
in the
onNewIntent()method. In single
top mode, only one instance of your activity is created and subsequent calls to start your activity do not create a new activity on the stack. This launch mode is useful so users can perform searches from the same activity without creating a new activity instance
every time.
相关文章推荐
- android在xml文件中实现背景或按钮颜色渐变
- Android RatingBar评分控件
- Android AttributeSet
- android布局--Android fill_parent、wrap_content和match_parent的区别
- Android Hardware Accelaration
- android-Updating Notifications,Using Big View Styles,Displaying Progress in a Notification
- Android开发人员必知的开发资源
- Android 设置主题实现点击波纹效果
- Android 实现ListView 内部的点击事件
- android 对sqlite数据库的增删改查等各种操作
- Android ProgressBar水平显示自定义样式
- android studio 签名
- Android自动化测试视频教程(robotium应用、SDK工具等)
- android *** Layout 02
- android 减少findViewById 的使用
- Android 蓝牙4.0(ble)开发的解决方案
- android中对View的onMeasure()方法的理解
- Android NDK开发基础——配置及简单案例
- Android流量监控类 trafficstats
- Android Studio修改默认字体大小