我正在尝试在 Android 3.0+ ActionBar 中添加对 SearchView
的支持,但我无法让 OnCloseListener
工作。
这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
searchView = (SearchView) menu.findItem(R.id.search_textbox).getActionView();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
searchLibrary(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) { return false; }
});
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
System.out.println("Testing. 1, 2, 3...");
return false;
}
});
return true;
}
搜索效果很好,除了 OnCloseListener
之外的所有搜索都可以正常工作。没有任何内容被打印到 Logcat。这是我按下“关闭”按钮时的 Logcat:
02-17 13:01:52.914: I/TextType(446): TextType = 0x0
02-17 13:01:57.344: I/TextType(446): TextType = 0x0
02-17 13:02:02.944: I/TextType(446): TextType = 0x0
我浏览过 the documentation和 sample ,但似乎没有任何改变。我在 Ice Cream Sandwich 上的华硕 Transformer Prime 和 Galaxy Nexus 上运行它。有什么想法吗?
更新:
是的 - System.out.println()
确实 工作。这是证据:
@Override
public boolean onQueryTextChange(String newText) {
System.out.println(newText + "hello");
searchLibrary(newText);
return false;
}
此 Logcat 中的结果:
02-17 13:04:20.094: I/System.out(21152): hello
02-17 13:04:24.914: I/System.out(21152): thello
02-17 13:04:25.394: I/System.out(21152): tehello
02-17 13:04:25.784: I/System.out(21152): teshello
02-17 13:04:26.064: I/System.out(21152): testhello
最佳答案
我也遇到这个问题,只好放弃“oncloselistener”。相反,您可以获取 menuItem,然后获取 setOnActionExpandListener
。然后覆盖未实现的方法。
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
Log.d("*******","onMenuItemActionExpand");
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
Log.d("*******","onMenuItemActionCollapse");
return true;
}
https://stackoverflow.com/questions/9327826/