我想知道在点击Android中的手机菜单按钮后是否有办法打开上下文菜单栏。我根据这个基本教程创建了一个上下文菜单栏:tutorial contextual menu bar
现在我的活动中有以下内容:
mCallback = new Callback(){
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case 1:
Toast.makeText(getBaseContext(), "Selected Action1 " + postId, Toast.LENGTH_SHORT).show();
//mode.finish(); // Automatically exists the action mode, when the user selects this action
break;
case 2:
Toast.makeText(getBaseContext(), "Selected Action2 " + postId, Toast.LENGTH_SHORT).show();
//mode.finish(); // Automatically exists the action mode, when the user selects this action
break;
case 3:
Toast.makeText(getBaseContext(), "Selected Action3 " + postId, Toast.LENGTH_SHORT).show();
//mode.finish(); // Automatically exists the action mode, when the user selects this action
break;
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Actions for Post");
getMenuInflater().inflate(R.menu.context_menu_posts, menu);
menu.add(1, 1, 1, "Option 1");
menu.add(1, 2, 2, "Option 2");
menu.add(1, 3, 3, "Option 3");
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mMode = null;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
};
但现在我希望它在用户点击菜单按钮时“显示”。我在LongclickListener中使用此代码之前:
if(mMode!=null)
return false;
else
mMode = startActionMode(mCallback);
这对于Activity中的控件非常有用,但有没有办法在菜单点击中执行此操作?我尝试在@Overriden菜单方法中放入startActionMode的代码,但是点击菜单按钮后我无法打开它。
提前致谢!
答案 0 :(得分:0)