调用系统的搜索框

来源:互联网 发布:部门预算软件 编辑:程序博客网 时间:2024/05/16 02:38

转自 http://www.maxiaoguo.com/shipin/237.html


1、最简单的搜索框:

在res文件夹下新建一个xml文件夹  ,其中的文件命名为  searchable.xml,内容如下,注意!!label下的String千万千万别直接写,要调用资源文件的String

要不你找去吧,我找了一天都没找到什么愿意


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


其中的配置文件


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.cs.search"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity android:name="org.cs.search.SearchActivity">  <!--这个是过滤搜索事件的,这个有没有必要加还没验证呢--> 
   <intent-filter >
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
</activity>

          <meta-data
            android:name="android.app.default_searchable" <!--别的页面点击搜索按钮的时候也调用org.cs.search.SearchActivity这个的-->
            android:value="org.cs.search.SearchActivity" /> 
   
    </application>
</manifest>


2,添加搜索建议的搜索框


贴代码   mainsearch.java


package com.m4399.com;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainSearch extends Activity{  
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

View search = this.findViewById(R.id.main_search);
if(search!=null){
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置这个界面的搜索按钮的点击事件
onSearchRequested();
}
});
}
}


@Override
public boolean onSearchRequested(){
Bundle bundle=new Bundle();
//打开浮动搜索框(第一个参数默认添加到搜索框的值)
//bundle为传递的数据
startSearch("", false, bundle, false);
//这个地方一定要返回真 如果只是super.onSearchRequested方法
//不但onSearchRequested(搜索框默认值)无法添加到搜索框中
//bundle也无法传递出去
return true;
}
}  


SearchResultActivity.java   

处理搜索结果的


package com.m4399.com;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class SearchResultActivity extends Activity{  
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.my_search);
Intent intent = this.getIntent();
String string = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(string, null);

TextView textView = (TextView) findViewById(R.id.message);
textView.setText(string);

}

@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
//获得搜索框里值
String query=intent.getStringExtra(SearchManager.QUERY);
//保存搜索记录
SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(query, null);
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
//获取传递的数据
/*Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata=bundled.getString("data");
tvdata.setText(ttdata);


}else{
tvdata.setText("no data"); 
}*/
}
}
}  



SearchSuggestionSampleProvider.java

这个这个是。。。应该是共享数据用的吧


package com.m4399.com;
import android.content.SearchRecentSuggestionsProvider;
public class SearchSuggestionSampleProvider extends SearchRecentSuggestionsProvider{
public final static String AUTHORITY = "com.m4399.com.SearchSuggestionSampleProvider";
public final static int MODE = DATABASE_MODE_QUERIES;


public SearchSuggestionSampleProvider() {
super();
setupSuggestions(AUTHORITY, MODE);
}
}




res---->xml--->searchble.xml


<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/label"    //这里要注意啊,坑爹啊
    android:hint="@string/hint"
  android:searchMode="showSearchLabelAsBadge"
    android:searchSuggestAuthority="com.m4399.com.SearchSuggestionSampleProvider"
    android:searchSuggestSelection=" ? ">
</searchable>



配置文件  


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.m4399.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name="com.m4399.com.MainSearch"
                  android:label="@string/app_name"
                  android:launchMode="singleTask"
                  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchResultActivity"
            android:windowSoftInputMode="adjustPan"
            android:theme="@android:style/Theme.Light"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

         <!-- 声名在本应用程序中都可以调用onSearchRequested()进行搜索 -->
        <meta-data
            android:name="android.app.default_searchable"
            android:value="com.m4399.com.SearchResultActivity" />
         
         
<provider android:name="SearchSuggestionSampleProvider" android:authorities="com.m4399.com.SearchSuggestionSampleProvider"></provider>
</application>
    <uses-sdk android:minSdkVersion="10" />
</manifest> 


其中用provider的时候容易出现一个异常

=Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER
[2012-03-30 11:42:37 - Apollo] Please check logcat output for more details.
[2012-03-30 11:42:37 - Apollo] Launch canceled!


原因是其中的privider已经存在了,可能是你demo中测试的时候的provider跟项目中的privider是同名的,把其中的一个去掉就可以了

低级错误
注意配置文件中的包名一定要对应,要不报错


3,自定义搜索框 加搜索建议


待你们完善。。。。。。。

原创粉丝点击