Android 使用 actionLayout (SherlockActionBar) 时未调用 onOptionsItemSelected
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11627892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
提问by user634545
The method onOptionsItemSelected isn't being called when using actionLayout in a menu item. Am I missing something, or is it a known problem with SherlockActionBar?
在菜单项中使用 actionLayout 时不会调用 onOptionsItemSelected 方法。我是不是遗漏了什么,还是 SherlockActionBar 的一个已知问题?
Activity
活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.article, menu);
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.menu_item_comment:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Menu
菜单
<item android:id="@+id/menu_item_comment"
android:showAsAction="ifRoom"
android:actionLayout="@layout/action_bar_comment_layout"/>
回答by Tomislav Novoselec
well, you have to set onClickListener on that actionLayout to receive callback. I do it like this:
好吧,您必须在该 actionLayout 上设置 onClickListener 以接收回调。我这样做:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.map_menu, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.menu_more) {
itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(this);
}
}
}
return super.onCreateOptionsMenu(menu);
}
回答by Luten
You'll have to add your own OnClickListener
and explicitly call onOptionsItemSelected
:
您必须添加自己的OnClickListener
并显式调用onOptionsItemSelected
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem awesomeMenuItem = menu.findItem(R.id.action_awesome);
View awesomeActionView = menuItem.getActionView();
awesomeActionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(awesomeMenuItem));
}
});
}
P.S: Don't know why it doesn't work out of the box.
PS:不知道为什么它不能开箱即用。
回答by Arun Kumar
You should use MenuItemCompat.getActionView(menuItem);instead of item.getActionView();if you are developing for older version.
你应该使用MenuItemCompat.getActionView(menuItem); 而不是item.getActionView(); 如果您正在为旧版本开发。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
for (int i = 0; i< menu.size() ;i++) {
MenuItem menuItem = menu.getItem(i);
if (menuItem.getItemId() == R.id.add_item) {
View view = MenuItemCompat.getActionView(menuItem);
if (view != null) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ToDoActivity.class);
startActivity(intent);
}
});
}
}
}
return true;
}
回答by Alexey Timokhin
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
View view = menu.findItem(R.id.menu_item_comment).getActionView();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
return true;
}
Also, (and that was very important for me, so other answers did not work) you need to disable the clickable option of all views in your action layout (that is, action_bar_comment_layout.xml):
此外,(这对我来说非常重要,所以其他答案不起作用)您需要禁用操作布局中所有视图的可点击选项(即 action_bar_comment_layout.xml):
android:clickable="false"
回答by Sumit Anantwar
Combining @Arun Kumar's and @Luten's answers, the below method will make the implementation generic. For all the menu items using actionView, we setOnClickListenerto call onOptionsItemSelected(item). This way we can mix and match normal and actionLayout menu items, without worrying about setting individual onClickListeners.
结合@Arun Kumar 和@Luten 的答案,下面的方法将使实现变得通用。对于所有使用actionView的菜单项,我们setOnClickListener来调用onOptionsItemSelected(item)。这样我们就可以混合搭配 normal 和 actionLayout 菜单项,而不必担心设置单独的 onClickListeners。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(menuResourceId(), menu);
for (int i = 0; i < menu.size(); i++) {
final MenuItem item = menu.getItem(i);
View actionView = MenuItemCompat.getActionView(item);
if (actionView != null) {
actionView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
onOptionsItemSelected(item);
}
});
}
}
super.onCreateOptionsMenu(menu, inflater);
}
回答by Tijo Thomas
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_add_require, menu)
val menuItem = menu!!.findItem(R.id.menu_cart)
val view = menuItem.actionView
view.setOnClickListener {
onOptionsItemSelected(menuItem)
}
return true
}
Working for me (code is in kotlin)
为我工作(代码在 kotlin 中)