如何处理searchview小部件中后退箭头上的click事件:
我尝试了这段代码,但它不起作用:
searchtollbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "back arrow clicked");
}
});
我也试过这个:
MenuItemCompat.setOnActionExpandListener(item_search, new
MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true;
}
});
上面代码的问题是调用onMenuItemActionCollapse()方法也执行onQueryTextChange(),这是不可取的。
请帮忙。
答案 0 :(得分:1)
要处理您必须覆盖onOptionsItemSelected
方法。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
// handle back event.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 1 :(得分:0)
试试
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Log.d( TAG, "expand" );
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Log.d( TAG, "collapse" );
return true;
}
});