gpt4 book ai didi

android - 检测点击操作栏后退按钮 -(点击操作栏后退按钮时 OnOptionsItemSelected 不调用)

转载 作者:可可西里 更新时间:2023-11-01 19:03:09 26 4
gpt4 key购买 nike

我有一个包含 searchview 的操作栏。当用户单击搜索按钮并折叠搜索 View 时,操作栏会在左侧显示一个后退按钮。

enter image description here

我们如何检测用户何时点击了这个后退按钮?

编辑

根据答案,我检查了我的 OnOptionsItemSelected 但它也没有调用。这是我的 OnOptionsItemSelected 的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);

}
return true;
}
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}

最佳答案

把它放在 onCreateOptionsMenu 方法上:

MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {

return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {

//DO SOMETHING WHEN THE SEARCHVIEW IS CLOSING

return true;
}
});

关于android - 检测点击操作栏后退按钮 -(点击操作栏后退按钮时 OnOptionsItemSelected 不调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26376429/

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