gpt4 book ai didi

android - 使用 Sherlock ActionBar SearchView setOnKeyListener 进行搜索

转载 作者:行者123 更新时间:2023-11-29 14:52:11 25 4
gpt4 key购买 nike

我正在尝试 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;
}

最佳答案

避免做这样的事情。使用 SearchView.OnQueryTextListener

final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries..");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextChange(String newText) {
return false;
}


@Override
public boolean onQueryTextSubmit(String query) {
if (query.length() != 0) {
System.out.println("--->" + query);
// handle search here
return true;
}
return false;
}
});

始终阅读 the docs

关于android - 使用 Sherlock ActionBar SearchView setOnKeyListener 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14772464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com