• Android 浮动搜索框 searchable 使用(转)。


    Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用。
    search dialog 只能为于activity窗口的上方。下面以点击EditText输入框启动search dialog搜索框为例:
    效果如下

    search1

    实现步骤:

    1. 新建searchable.xml配置文件,放在res/xml目录下。
    searchable.xml用于配置搜索框相关属性,配置文件内容为:

       <?xml version="1.0" encoding="utf-8"?>
       <searchable xmlns:android="http://schemas.android.com/apk/res/android"
           android:label="@string/app_name"
           android:hint="@string/search_hint"/>

    注:android:lable是唯一必须定义的属性。它指向一个字符串,是应用程序的名字。
    实际上该label也只有在search suggestions for Quick Search Box可用时才可见。
    android:hint属性不是必须,但是还是推荐总是定义它。它是search box用户输入前输入框中的提示语。

    其它属性可以查看google官方文档:

    <?xml version="1.0" encoding="utf-8"?>
        <searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="string resource"
            android:hint="string resource"
            android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
            android:searchButtonText="string resource"
            android:inputType="inputType"
            android:imeOptions="imeOptions"
            android:searchSuggestAuthority="string"
            android:searchSuggestPath="string"
            android:searchSuggestSelection="string"
            android:searchSuggestIntentAction="string"
            android:searchSuggestIntentData="string"
            android:searchSuggestThreshold="int"
            android:includeInGlobalSearch=["true" | "false"]
            android:searchSettingsDescription="string resource"
            android:queryAfterZeroResults=["true" | "false"]
            android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
            android:voiceLanguageModel=["free-form" | "web_search"]
            android:voicePromptText="string resource"
            android:voiceLanguage="string"
            android:voiceMaxResults="int"
            >
            <actionkey
                android:keycode="KEYCODE"
                android:queryActionMsg="string"
                android:suggestActionMsg="string"
                android:suggestActionMsgColumn="string" >
        </searchable>

    2. 在AndroidManifest.xml文件中声明Searchable Activity。
    Searchable Activity为搜索结果显示Activity,可以定义为搜索框所在的当前Activity,也可以单独定义一个Activity
    这里直接定义当前搜索框所在SearchActivity.配置如下:

      
    
       <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
           <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchActivity"/>
    
            <activity android:name=".SearchActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                </intent-filter>
    
                <meta-data
                    android:name="android.app.searchable"
                    android:resource="@xml/searchable" />
            </activity>
    
        </application>
       

    注:activity标签内的标签必须包括android:name这个属性,而且其值必须为”android.app.searchable”,
    还必须包括android:resource这个属性,它指定了我们的search dialog的配置文件。(res/xml/searchable.xml).

    3.启动搜索框search dailog:
    在activity中调用onSearchRequested()方法

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.search_edit:
                    onSearchRequested();
                    break;
            }
        }
     

    4. 获取搜索关键字
    搜索框中输入的搜索关键字通过下面代码可以取到:
    String query = intent.getStringExtra(SearchManager.QUERY);
    但在获取前应该先判断Intent中action是否为搜索action:”android.intent.action.SEARCH”

            Intent intent = getIntent();
            if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                String query = intent.getStringExtra(SearchManager.QUERY);
              
            }
      

    5.搜索结果处理方式
    在前面已经提过,搜索结果可以在单独一个类里处理,也可以在当前搜索框所在类处理,如果在当前搜索框所在类处理,需设置当前类为SingTop模式,防止再次创建Activity. 但这样又会引发一个问题,搜索时onCreate方法不会在执行,而在可以执行的onResult方法中得到的Intent不包含搜索Action:”android.intent.action.SEARCH”,而是原来的Action。
    这说明搜索执行后Intent已经被改变,Activity中通过getIntent()取到的Intent还是原来的Intent。那么被改变的Intent从那里获取呢?
    重写 onNewIntent(Intent intent) 获取,执行 setIntent(intent) 更新Activity中Intent,

     
        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
                String query = intent.getStringExtra(SearchManager.QUERY);
            }
       }
    

    综上在当前搜索框所在类获取搜索关键字处理搜索结果可以这样写:

     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search_dialog_layout);
            mSearchEdit = (EditText) findViewById(R.id.search_edit);
            mContentTxt = (TextView)findViewById(R.id.search_content_txt);
            mSearchEdit.setOnClickListener(this);
            handleIntent(getIntent());
        }
    
        /**
         * 重复刷新当前Activity时执行
         * @param intent
         */
        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
            handleIntent(intent);
        }
    
        private void handleIntent(Intent intent) {
            if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
                String query = intent.getStringExtra(SearchManager.QUERY);
                doMySearch(query);
            }
        }
        /**
         *   处理搜索结果
         */
        public void doMySearch(String query){
            mContentTxt.setText(query);
        }
  • 相关阅读:
    c++重点笔记2
    c++学习笔记重点1
    创业思路(3) 传统行业与互联网思维
    创业思路(2) 社交
    创业思路(1)
    Asp.Net实现Http长连接推送
    又回来了
    2021.10.23软件更新公告
    2021.10.22软件更新公告
    SharePoint 2013 新特性 (三) 破改式 —— 设计管理器的使用 [2.HTML变身模板页]
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/5220994.html
Copyright © 2020-2023  润新知