android-Adding Search Functionality,Setting Up the Search Interface
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 withSearchView
,
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 SearchView
widget 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 SearchView
to
show at all times, only when there is room, or as a collapsible action, which displays the SearchView
as
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 collapsibleActionView
attribute 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.
onCreateOptionsMenu()
method
that you created before, associate the searchable configuration with theSearchView
by
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_SEARCH
intent
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_SEARCH
intent
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.
相關文章
- Elasticsearch中URI Search和RequestBody Search分析Elasticsearch
- JavaScript search()JavaScript
- PDF Search mac如何使用?PDF Search使用教程Mac
- LintCode-Search Range in Binary Search Tree
- Elasticsearch Search APIElasticsearchAPI
- 查詢 Search
- URL search 屬性
- elasticsearch(八)---search apiElasticsearchAPI
- Elasticsearch——Filter search resultsElasticsearchFilter
- elasticsearch之search templateElasticsearch
- Linux Packages SearchLinuxPackage
- Elastic Search Java ApiASTJavaAPI
- Search Insert Position
- google search grammerGo
- Metasploit search命令使用技巧
- Sphinx Search 學習 (一)
- Leetcode Word SearchLeetCode
- tpextbuilder- Search 搜尋UI
- PDF Search mac如何使用?Mac
- Selective Search for Object RecognitionObject
- InnoDB search原理解析
- Symbol.search 屬性Symbol
- Leetcode Search for a RangeLeetCode
- DevNow: Search with LunrjsdevJS
- elastic search服務搭建AST
- Elasticsearch——search相關知識Elasticsearch
- HTML input search搜尋域HTML
- Leetcode-Word SearchLeetCode
- LeetCode 34 Search for a RangeLeetCode
- Elastic Search 索引 API 介紹AST索引API
- LintCode-Search for a Range
- Leetcode-Search for a rangeLeetCode
- Word Search leetcode javaLeetCodeJava
- Search for a Range leetcode javaLeetCodeJava
- Oracle 安裝Full Text searchOracle
- 新的Google Ajax Search ApiGoAPI
- re.search()用法詳解
- SAP CRM Advanced search和Simple search裡Max hit表現行為的差異