博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Setting Up the Search Interface 设置搜索界面
阅读量:4046 次
发布时间:2019-05-24

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

Beginning in Android 3.0, using the widget as an item in the action bar is the preferred way to provide search in your app. Like with all items in the action bar, you can define the to show at all times, only when there is room, or as a collapsible action, which displays the as an icon initially, then takes up the entire action 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 .

Add the Search View to the Action Bar

To add a widget to the action bar, create a file named res/menu/options_menu.xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item. The collapseActionView attribute allows your to expand to take up the whole action bar and collapse back down into a normal action bar item when not in use. Because of the limited action bar space on handset devices, using the collapsibleActionView attribute is recommended to provide a better user experience. http://blog.sina.com.cn/mtkshanghai

Note: If you already have an existing XML file for your menu items, you can add the <item> element to that file instead.

To display the in the action bar, inflate the XML menu resource (res/menu/options_menu.xml) in the method of your activity:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.options_menu, menu);    return true;}

If you run your app now, the appears in your app's action bar, but it isn't functional. You now need to define how the behaves.

Create a Searchable Configuration

A defines how the behaves and is defined in a res/xml/searchable.xml file. At a minimum, a searchable configuration must contain an android:label attribute that has the same value as the android:label attribute of the or element in your Android manifest. However, we also recommend adding an android:hint attribute to give the user an idea of what to enter into the search box:

In your application's manifest file, declare a element that points to the res/xml/searchable.xml file, so that your application knows where to find it. Declare the element in an <activity> that you want to display the in:

...

In the method that you created before, associate the searchable configuration with the by calling :

@Overridepublic 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;}

The call to obtains a object that is created from the searchable configuration XML file. When the searchable configuration is correctly associated with your , the starts an activity with the intent when a user submits a query. You now need an activity that can filter for this intent and handle the search query.

Create a Searchable Activity

A tries to start an activity with the when a user submits a search query. A searchable activity filters for the intent and searches for the query in some sort of data set. To create a searchable activity, declare an activity of your choice to filter for the intent:

...
...

In your searchable activity, handle the intent by checking for it in your method.

Note: If your searchable activity launches in single top mode (android:launchMode="singleTop"), also handle the intent in the 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.

public class SearchResultsActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        ...        handleIntent(getIntent());    }    @Override    protected void onNewIntent(Intent intent) {        ...        handleIntent(intent);    }    private void handleIntent(Intent intent) {        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {            String query = intent.getStringExtra(SearchManager.QUERY);            //use the query to search your data somehow        }    }    ...}

If you run your app now, the can accept the user's query and start your searchable activity with the intent. It is now up to you to figure out how to store and search your data given a query.

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

你可能感兴趣的文章
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
Java的对象驻留
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
如何构建高扩展性网站
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>