我正在疯狂地试图弄清楚如何使搜索栏(在ActionBar中)正常工作但仍保持与姜饼的兼容性。我遇到的第一个问题是,只是将v7支持库jar放在libs文件夹中,或者将其作为依赖项添加,都不起作用:我在菜单中遇到错误xml"找不到资源标识符for attribute' actionViewClass'在包...我认为这是因为jar中没有资源。我发现解决第一个问题的唯一方法是下载实际的v7支持项目,导入Eclipse,然后让我的项目指向v7库。这是唯一的方法吗?
其次,我不能为我的生活做出onNewIntent在活动中的召唤。单击搜索图标时,搜索编辑文本打开正常。然后我输入一些文本并单击搜索键(或输入SIM卡),没有任何反应。应该调用onNewIntent,因为我已将活动设置为清单中的搜索意图,但没有任何反应。任何帮助将不胜感激。
我的菜单xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/menu_search"
android:icon="@drawable/ic_menu_search"
android:title="@string/search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always" />
</menu>
我的清单:
<activity android:name="com.random.search.SearchBarCompatActivity"
android:launchMode="singleTop"
android:theme="@style/Theme/AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
我的活动:
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.TextView;
import android.support.v7.app.ActionBarActivity;
import com.random.R;
public class SearchBarCompatActivity extends ActionBarActivity
{
@Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setContentView(R.layout.fragment_and_activity_container);
TextView mDescriptionBox = (TextView)findViewById(R.id.desc_box);
if (mDescriptionBox != null)
mDescriptionBox.setText(getString(R.string.search_bar_compat_desc));
// Load main fragment
FragmentManager manager = getSupportFragmentManager();
Fragment mainFragment = manager.findFragmentById(R.id.fragment_container);
if (mainFragment == null) {
mainFragment = new SearchBarCompatFragment();
manager.beginTransaction().add(R.id.fragment_container, mainFragment).commit();
}
}
/** On New Intent
*
* Called automatically when a new search string has been sent. Calls the
* fragment to display the string in a Toast.
*
* @param intent
*/
@Override
public void onNewIntent(Intent intent)
{
Log.d("Activity", "New Intent received.");
setIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
SearchBarCompatFragment frag = (SearchBarCompatFragment)getSupportFragmentManager()
.findFragmentById(R.id.fragment_container);
frag.showSearchString(query);
}
}
}
我的片段:
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.random.R;
public class SearchBarCompatFragment extends Fragment
{
private Context mContext;
private SearchView mSearchField;
/** On Create */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.mContext = getActivity();
// Enable menu
setHasOptionsMenu(true);
}
/** On Create Options Menu
*
* Inflates the menu
*
* @param Menu menu
* @param MenuInflater inflater
*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.search_bar_compat_menu, menu);
Log.d("Fragment", "Creating options menu...");
mSearchField = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
if (mSearchField != null)
{
mSearchField.setIconifiedByDefault(false);
SearchManager sm = (SearchManager) getActivity().getSystemService(Activity.SEARCH_SERVICE);
mSearchField.setSearchableInfo(sm.getSearchableInfo(getActivity().getComponentName()));
mSearchField.setQuery(getActivity().getIntent().getStringExtra(SearchManager.QUERY), false);
}
}
/** On Options Item Selected
*
* Handle Quit and Sync menu items.
*
* @param MenuItem item
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
Log.d("Fragment", "onOptionsItemSelected called");
switch(item.getItemId())
{
case R.id.menu_search:
getActivity().onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Show Search String
*
* Called from Activity when a search string is returned. Shows
* that a Fragment can deal with the results.
*
* @param query
*/
public void showSearchString(String query)
{
Log.d("Fragment", "Displaying toast...");
Toast.makeText(mContext, "Search string: " + query, Toast.LENGTH_SHORT).show();
}
}
我可搜索的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" >
</Searchable>
答案 0 :(得分:0)
对于任何想要了解的人。 F%^ k!@g问题是&#34; Searchable&#34;标记在s earchable.xml
文件中是大写的。 它必须全部小写。谢谢Eclipse告诉我!