Android 搜索活动从未被调用过



在我发布这个之前,我尝试了这里发布的所有可能的解决方案。最后我被困住了,没有运气。我只想在搜索活动中显示查询字符串。有什么帮助吗?提前谢谢。

安卓清单.xml :

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/my_app"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="android.app.default_searchable"
android:value="com.mycompany.mygoodapp.SearchResultsActivity"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/my_app"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultsActivity"
android:label="@string/my_app"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>

搜索结果活动实现如下:

public class SearchResultsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
DisplayToast("Here ...");
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
DisplayToast(query);
}
}
public void DisplayToast(String s) {
Toast.makeText(SearchResultsActivity.this,
s, Toast.LENGTH_LONG).show();
}
}

可搜索.xml :

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

主要活动 :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is   present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
ComponentName cn = new ComponentName("com.mycompany.mygoodapp", "com.mycompany.mygoodapp.SearchResultsActivity");
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
searchView.setIconifiedByDefault(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
///  
else if(id==R.id.search)
{
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}

主菜单布局搜索菜单项:

<item
android:id="@+id/search"
android:title="@string/search_title"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">

您可以通过两种方式搜索活动

1 路

菜单/options_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/disha"
app:showAsAction="collapseActionView|always"
android:title="search"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

适用于您活动的 Java 代码

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}

将其添加到您最重要的文件中

<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />

可搜索.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="search hint" />

2 使用自定义操作栏的方式,或者您也可以尝试这个

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setCustomView(R.layout.search_layout);
search = (EditText) getSupportActionBar().getCustomView().
findViewById(R.id.searchfield);
search.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
return true;
}
});
getSupportActionBar().setDisplayOptions(getSupportActionBar().DISPLAY_SHOW_CUSTOM
| getSupportActionBar().DISPLAY_SHOW_HOME);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

而不是像这样创建自定义 Actiob 条形布局

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_search_edit"
android:hint="@string/search_hint"
android:inputType="textFilter"
android:textColor="@color/colorBlack"
android:textColorHint="@color/colorAccent"
android:textSize="15sp" />

试试这个,如果有任何疑问,请问我

最新更新