我在让 Android 搜索界面在操作栏中工作时遇到问题
我尝试按照此处的指南进行操作,但我遇到了一些问题。我正确地让搜索图标显示在操作栏中,但我在将 它附加到 SearchView 时遇到问题。searchable configuration
我的分辨率/菜单/options_menu.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
我的 res/xml/searchable.xml文件(注意:我不得不在 res 文件夹中创建一个“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" />
我的 res/values/strings.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Starting To Feel It</string>
<string name="single_post">Single Post View</string>
<string name="search">Search Activity</string>
<string name="action_settings">Settings</string>
<string name = "pagination_last">Last</string>
<string name = "pagination_next">Next</string>
<string name = "player_play">Play</string>
<string name = "player_previous">Previous</string>
<string name = "player_next">Next</string>
<string name = "player_playlist">Playlist</string>
<string name = "player_progress_bar">Progress Bar</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="search_title">Search STFI</string>
<string name="search_hint">Search STFI</string>
<string name="menu_item_picture">Menu Item Picture</string>
</resources>
My AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stfi"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".StartingToFeelIt"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.SinglePost"
android:label="@string/single_post">
</activity>
<activity
android:name=".activities.SearchActivity"
android:label="@string/search">
<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>
我的所有活动都扩展了我调用的类,下面是在 MenuActivity 中定义的函数.java:MenuActivity
onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
System.out.println("The component name is " + this.getComponentName().toString());
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
if(info == null)
{
System.out.println("It is null!");
}
searchView.setSearchableInfo(info);
return true;
}
我总是得到这个信息在这个函数是空的。我不确定我做错了什么来获取信息是否为空,我也不确定如何解决它。我不确定你是否需要它,但下面是我的SearchActivity类。现在,它不执行任何操作,但尝试显示搜索(但是,由于搜索配置设置不正确,因此永远不会调用它)。该文件保存在 com.stfi.activities.SearchActivity.java 中。
另一个注意事项:当我在上面的函数中打印出组件名称时,我得到的不仅仅是com.stfi.StartingToFeelIt。这导致我相信我的AndroidManifest文件有问题,但我不知道这是否是我无法附加搜索配置的原因。The component name is ComponentInfo(com.stfi/com.stfi.StartingToFeelIt)
public class SearchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
System.out.println("You are searchin " + query);
}
}
}