当前位置:操作系统 > 安卓/Android >>

Android中的Searchview以及SearchableDictionary项目和plurals详解

Android4.0之后,Android内置了一个搜索控件,配合ActionBar上面的搜索按钮,相当不错好看,这次使用了下,觉得很不错。
这个搜索的好处在于你点击后,他会自动弹出个搜索框,输入内容后会自动弹出匹配的内容,形成一个列表,选择后会弹到你想要去的界面。
类似这样的
 
 
你需要在代码中的onCreateOptionsMenu中加入
   SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
 
然后在onOptionsItemSelected中加入
 case R.id.search:
                onSearchRequested();
                return true;
这样才会执行搜索功能。
 
从开始看代码
 handleIntent(getIntent());这个方法在onCreate中就执行了
并且在
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }
也执行了,说明这个方法比算较重要,其实这个是一个搜索按钮用的因为在android4.0以上系统 输入法里有个“搜索”按钮或者是“前往”按钮
这样的搜索按钮,这个 handleIntent(getIntent())主要就是执行的这个搜索
看里面的代码:
    private void handleIntent(Intent intent) {
    //Intent.ACTION_VIEW  android:searchSuggestIntentAction="android.intent.action.VIEW"  还在这个view中
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            // handles a click on a search suggestion; launches activity to show word
            Intent wordIntent = new Intent(this, WordActivity.class);
            wordIntent.setData(intent.getData());
            startActivity(wordIntent);
            finish();
            
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // handles a search query  Intent.ACTION_SEARCH这个Intent是在当我点击系统的搜索框右面的按钮时触发的
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
        }
    }
上面就是handleIntent里面的代码
  handleIntent里的代码有两个意思  一个就是当Intent是Intent.ACTION_VIEW.这个的时候跳到你指定的类中必然WordActivity,其实这个ntent.ACTION_VIEW就是你在搜索出建议后点击建议里面的列表进入的界面,比如你点击这个就执行的是这个intent,执行这个intent,会返回一个URI,可以使用intent.getData()这个方法得到这个返回的URI,这个URI是系统自己返回的。可以看配置文件searchable.xml这个文件,这个文件的代码如下:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/search_label"
        android:hint="@string/search_hint"
        android:searchSettingsDescription="@string/settings_description"
        android:searchSuggestAuthority="com.example.android.searchabledict.DictionaryProvider"
        android:searchSuggestIntentAction="android.intent.action.VIEW"
        android:searchSuggestIntentData="content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r"
        android:searchSuggestSelection=" ?"
        android:searchSuggestThreshold="1"
        android:includeInGlobalSearch="true"
        >
 </searchable>
 
就是一个XML文件,里面配置的是android:searchSuggestAuthority这个是在AndroidManifest.xml里面配置的provider,比如     
   <provider android:name=".DictionaryProvider"
                  android:authorities="com.example.android.searchabledict.DictionaryProvider" />
里面的android:searchSuggestIntentData配置的就是你要返回的intentData,比如这里面配的是content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r到时候你在
 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            // handles a click on a search suggestion; launches activity to show word
            Intent wordIntent = new Intent(this, WordActivity.class);
            wordIntent.setData(intent.getData());
            startActivity(wordIntent);
            finish();
            
        } 这个里面的intent.getData()就会是content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r/(你点击的那个Item的ID)也就是一个带ID的URI,至于这里的ID,是你在代码里赋值给他的 ,这个后面说。
当如果是你点击右下角的搜索按钮的时候执行的是ACTION_SEARCH这个intent,会执行showResults这个方法。
   private void showResults(String query) {
 
 
        Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, selection,
                                  new String[]{query}, null);
 
 
        if (cursor == null) {
            // There are no results
            mTextView.setText(getString(R.string.no_results, new Object[] {query}));
        } else {
            // Display the number of results
            int count = cursor.getCount();
            String countString = getResources().getQuantityString(R.plurals.search_results,
                                    count, new Object[] {count, query});
            mTextView.setText(countString);
 
 
            // Specify the columns we want to display in the result
            String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                           DictionaryDatabase.KEY_DEFINITION };
 
 
     
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,