首页 > 代码库 > 搜索栏 SearchDialog 的使用 不显示的问题

搜索栏 SearchDialog 的使用 不显示的问题

   在配置文件,代码里反复查看的情况下,还是未能找到SearchDialog不显示的原因!

在度娘找了很久,终于在某人的评论中找到答案,试了下了果然起效,修改内容如下:

<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/searchHint"   <!--需要用@string 方法,且必须要有label属性-->android:hint="@string/searchHint" android:searchMode="showSearchLabelAsBadge" > </searchable>

不错,正是android:label属性的赋值问题,需要用@String方式注入赋值,而不能直接赋值!

对于SearchDialog的使用,会比较繁琐些,但是它是一种浮动的方式出现,可以覆盖当前Activity界面,

相对于直接使用SearchView效果要好一些。

当然SearchView 如果跟Fragment结合,也是不错的选择!

 

下面介绍下SearchDialog的使用方法:

第一步:配置文件(xml)配置:

1:添加searchable显示的xml文件;

2:AndroidManifest.xml中添加配置参数;

 

第二步:编写代码,调用SearchDialog。

1:编写SearchRecentSuggestionsProvider 实体类,用于保存搜索记录。

2:在需要使用搜索的当前活动(Activity),覆盖onSearchRequested方法,调用startSearch方法显示SearchDialog。

3:在显示搜索结果的活动(Activity),重写onNewIntent方法,处理查询。

注意:

第一步,示例代码。

//定义intent-filter接收SEACH Action事件。<intent-filter>                <action android:name="android.intent.action.SEARCH" />                <category android:name="android.intent.category.DEFAULT" />     </intent-filter> <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data><?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/searchHint"   //需要用@string 方法,且必须要有label属性android:hint="@string/searchHint" android:searchMode="showSearchLabelAsBadge" > </searchable>指出了当执行搜索的字符串提交时,将调用哪一个activity去进行处理。<meta-data android:name="android.app.default_searchable" android:value=".YourSearchActivity" /> 
View Code

第二步,示例代码。

//调用SearchDialog    public boolean onSearchRequested() {        // If your application absolutely must disable search, do it here.// Next, set up a bundle to send context-specific search data (if any)        // The bundle can contain any number of elements, using any number of keys;        // For this Api Demo we copy a string from the user input field, and store        // it in the bundle as a string with the key "demo_key".        // For most applications, you can simply pass null to startSearch().        Bundle appDataBundle = null;         appDataBundle = new Bundle();         // Now call the Activity member function that invokes the Search Manager UI.        startSearch("111", false, appDataBundle, false);               // Returning true indicates that we did launch the search, instead of blocking it.        return true;    }

当按下搜索按钮,调用onSearchRequested方法,系统就会自动发送Intent,action是Intent.ACTION_SEARCH.

//处理查询内容:    /**     * Called when new intent is delivered.     *     * This is where we check the incoming intent for a query string.     *     * @param newIntent The intent used to restart this activity     */    @Override    public void onNewIntent(final Intent newIntent) {        super.onNewIntent(newIntent);               // get and process search query here        final Intent queryIntent = getIntent();        final String queryAction = queryIntent.getAction();        if (Intent.ACTION_SEARCH.equals(queryAction)) {            doSearchQuery(queryIntent, "onNewIntent()");        }        else {            Toast.makeText(this, "onNewIntent(), but no ACTION_SEARCH intent", Toast.LENGTH_SHORT).show();        }    } /**     * Generic search handler.     *     * In a "real" application, you would use the query string to select results from     * your data source, and present a list of those results to the user.     */    private void doSearchQuery(final Intent queryIntent, final String entryPoint) {      // The search query is provided as an "extra" string in the query intent        final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);           Toast.makeText(this,queryString, Toast.LENGTH_SHORT).show();               //保存搜索记录        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);        suggestions.saveRecentQuery(queryString, null);    }
 1 public class SearchSuggestionSampleProvider extends 2         SearchRecentSuggestionsProvider { 3      /** 4      * This is the provider authority identifier.  The same string must appear in your 5      * Manifest file, and any time you instantiate a  6      * {@link android.provider.SearchRecentSuggestions} helper class.  7      */ 8     final static String AUTHORITY = "com.example.testhttp.SuggestionProvider"; 9     /**10      * These flags determine the operating mode of the suggestions provider.  This value should 11      * not change from run to run, because when it does change, your suggestions database may 12      * be wiped.13      */14     final static int MODE = DATABASE_MODE_QUERIES;15     16     /**17      * The main job of the constructor is to call {@link #setupSuggestions(String, int)} with the18      * appropriate configuration values.19      */20     public SearchSuggestionSampleProvider() {21         super();22         setupSuggestions(AUTHORITY, MODE);23     }24 }

看了代码是不是有些熟悉,没错,来源Android SDK 的App Demo,所以写Android应用还是先浏览Android开发人员写的Demo,还是很有参考价值的!

 

搜索栏 SearchDialog 的使用 不显示的问题