问题
I'm trying to make an app that uses SearchView
compatible with devices running Android version 2.3 up to 4.4+. I currently have the following code inside an ActionBarActivity
, which works fine in devices running Honeycomb and above but not earlier devices:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconified(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
} else {
View searchView = SearchViewCompat.newSearchView(this);
SearchViewCompat.setOnQueryTextListener(searchView, new OnQueryTextListenerCompat() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
The problem I'm facing is that the SearchView UI shows up fine in earlier devices, but the OnQueryTextListenerCompat doesn't seem to get triggered ever. Am I missing something? Thanks.
回答1:
I've solved this problem by not using SearchViewCompat I tested it's compatability from Gingerbread and to the latest version
in my search_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="@+id/menu_item_search"
android:icon="@android:drawable/ic_menu_search"
android:orderInCategory="1"
android:title="@string/menu_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always|collapseActionView"/>
</menu>
and in my Main.xml
public class Main extends ActionBarActivity implements OnQueryTextListener {
private SearchView searchView;
private SearchManager searchManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_menu, menu);
View view = MenuItemCompat.getActionView(menu.findItem(R.id.menu_item_filter));
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Search...");
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);
searchView.setOnQueryTextListener(Main.this);
searchView.clearFocus();
return super.onCreateOptionsMenu(menu);
}
public boolean onQueryTextChange(String val) {
if(!val.isEmpty()){
Log.e(""+val);
}
return false;
}
@Override
public boolean onQueryTextSubmit(String val) {
Log.e(""+val);
return false;
}
}
来源:https://stackoverflow.com/questions/25597139/onquerytextlistenercompat-not-getting-triggered