我正在尝试在我的应用程序布局中使用 SearchView 在我的 android 应用程序中实现搜索功能。我故意选择将它放在布局中而不是在操作栏中。
问题是,一旦我输入搜索文本并按下键盘上的输入/搜索键,什么也没有发生。永远不会调用可搜索的意图。我觉得我一定错过了导致搜索不触发的东西
如果我没有在下面包含正确格式的代码,我深表歉意。请让我知道是否有任何其他信息会有所帮助。提前致谢!
显现
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lotr.arda.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".WebActivity"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".menuActivity"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<activity android:name=".searchActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
</manifest>
资源/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/searchhint" >
</searchable>
SearchActivity.java
package com.lotr.arda;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
public class searchActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
System.out.println(query);
//doMySearch(query);
}
}
}
menuActivity.java(searchview所在的布局)
package com.lotr.arda;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;
public class menuActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_page);
try{
AdView myad = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest();
myad.loadAd(adRequest);
SearchManager searchManager = (SearchManager)getSystemService(SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
SearchView sv = (SearchView)findViewById(R.id.searchView1);
sv.setSearchableInfo(searchableInfo);
Button alpha = (Button)this.findViewById(R.id.button1);
alpha.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(menuActivity.this, MainActivity.class);
startActivity(intent);
}});
}catch(Exception e) {System.out.println(e.toString());}
}
}