博客
关于我
android-Creating a Search Interface
阅读量:129 次
发布时间:2019-02-26

本文共 5014 字,大约阅读时间需要 16 分钟。

Android helps you implement the user interface with either a search dialog that appears at the top of the activity window or a search widget that you can insert in your layout. 

Other features available for the search dialog and widget include:

  • Voice search
  • Search suggestions based on recent queries
  • Search suggestions that match actual results in your application data
However, the search widget is available only in Android 3.0 (API Level 11) and higher.

Note: If you want, you can handle all user input into the search widget yourself, using various callback methods and listeners. This document, however, focuses on how to integrate the search widget with the system for an assisted search implementation. If you want to handle all user input yourself, read the reference documentation for  and its nested interfaces.

> The searchable configuration file must include the  element as the root node and specify one or more attributes. For example:

You don't need to implement the search functionality yet—just create an activity that you can declare in the manifest. Inside the manifest's 
 element:
  1. Declare the activity to accept the  intent, in an  element.
  2. Specify the searchable configuration to use, in a  element.

For example:

   
       
           
       
       
   
    ...

Intent intent = getIntent();    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {        String query = intent.getStringExtra(SearchManager.QUERY);      doMySearch(query);    }
The search dialog provides a floating search box at the top of the screen, with the application icon on the left. The search dialog can provide search suggestions as the user types and, when the user executes a search, the system sends the search query to a searchable activity that performs the search. However, if you are developing your application for devices running Android 3.0, you should consider using the search widget instead (see the side box).

   
   
       
           
       
       
   
   
   
       
       
   
    ...
> If you want every activity in your application to provide the search dialog, insert the above 
 element as a child of the 
 element, instead of each 

Note: If your app uses an , then you should not use the search dialog for your search interface. Instead, use the  as a collapsible view in the app bar.

. If, however, the current activity is the searchable activity, then one of two things happens:

  1. By default, the searchable activity receives the  intent with a call to  and a new instance of the activity is brought to the top of the activity stack. There are now two instances of your searchable activity in the activity stack (so pressing the Back button goes back to the previous instance of the searchable activity, rather than exiting the searchable activity).
  2. If you set android:launchMode to "singleTop", then the searchable activity receives the intent with a call to , passing the new  intent here. For example, here's how you might handle this case, in which the searchable activity's launch mode is "singleTop":
    @Overridepublic void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    setContentView(R.layout.search);    handleIntent(getIntent());}@Overrideprotected void onNewIntent(Intent intent) {        setIntent(intent);    handleIntent(intent);}private void handleIntent(Intent intent) {        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {          String query = intent.getStringExtra(SearchManager.QUERY);      doMySearch(query);    }}
Note:
 When you use the search widget as an action view, you still might need to support using the search dialog, for cases in which the search widget does not fit in the Action Bar. See the following section about
.

For example, if you're using a  as an action view in the , you should enable the widget during the  callback:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {      // Inflate the options menu from XML    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.options_menu, menu);    // Get the SearchView and set the searchable configuration    SearchManager searchManager = (SearchManager) (Context.SEARCH_SERVICE);    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();    // Assumes current activity is the searchable activity    searchView.setSearchableInfo(searchManager.getSearchableInfo());    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default    return true;}
Note:
 Carefully consider whether voice search is appropriate for your application. All searches performed with the voice search button are immediately sent to your searchable activity without a chance for the user to review the transcribed query. Sufficiently test the voice recognition and ensure that it understands the types of queries that the user might submit inside your application.

转载地址:http://rwdk.baihongyu.com/

你可能感兴趣的文章
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>