SearchView 不调用 onOptionsItemSelected [英] SearchView Not call onOptionsItemSelected
问题描述
错误"好奇怪 其他时候我已经这样做了,但从未解决过这个问题......
"Error" Well Strange Other times I've done it and never tive this problem ...
如果我将appname:actionViewClass="android.support.v7.widget.SearchView"放在菜单上,我的搜索视图不会调用 onOptionsItemSelected.
My searchview not call onOptionsItemSelected if i put "appname:actionViewClass="android.support.v7.widget.SearchView" on menu.
清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<activity
android:name=".DashBoardActivity"
android:label="">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
dash_board.xml(菜单)(如果我撤回他调用的 actionViewClass(onOptionsItemSelected),但如果我放他不调用)
dash_board.xml (menu) (if I retreat actionViewClass he calls(onOptionsItemSelected ), but if i put he dont call)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appname="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.servic.DashBoardActivity" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
appname:showAsAction="always"
appname:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
appname:showAsAction="always"/>
</menu>
仪表板活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dash_board, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case R.id.action_search:
mSearchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
ToastMensagem("Digite um endereço.");
} else {
BuscarEndereco(mSearchView.getQuery().toString());
}
}
});
break;
}
return super.onOptionsItemSelected(item);
}
错误是,不调用 onOptionsItemSelected,我需要操作 searchview
The error is, not call onOptionsItemSelected, i need manipulate searchview
推荐答案
请注意,提交搜索结果不应该使用 SearchView.setOnQueryTextFocusChangeListener
Just to note that for submitting search result you should not use SearchView.setOnQueryTextFocusChangeListener
应该使用 SearchView.setOnQueryTextListener
代替.
以下是搜索提交侦听器的示例:
Below is an example for Search Submit listener:
case R.id.action_search:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(),
getString(R.string.abc_searchview_description_submit),
Toast.LENGTH_SHORT)
.show();
updateUI(query);
return true;
}
@Override
public boolean onQueryTextChange(String query) {
// Clear search
if (query.equals("")) {
updateUI(null);
}
return true;
}
});
这篇关于SearchView 不调用 onOptionsItemSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!