ActionBarSherlock SearchView を試しています。残念ながら
public boolean onKey(View arg0, int arg1, KeyEvent event)
解雇されません。その理由を知っていますか?この投稿 SearchView imeOptions and onQueryTextSubmit support から、ユーザーが別の方法で問題を解決したことがわかります。多分私は同じことをすべきですか?
ありがとう
これが私のコードです:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries…");
System.out.println("searchView.getQuery()"+searchView.getQuery());
searchView.setOnKeyListener(new OnKeyListener()
{
/**
* This listens for the user to press the enter button on
* the keyboard and then hides the virtual keyboard
*/
@Override
public boolean onKey(View arg0, int arg1, KeyEvent event) {
// If the event is a key-down event on the "enter" button
System.out.println("--->"+searchView.getQuery());
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(arg1 == KeyEvent.KEYCODE_ENTER) )
{
InputMethodManager imm = (InputMethodManager) SearchViews.this.getSystemService(SearchViews.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
return true;
}
return false;
}
} );
menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}