嗨,我正在尝试在一项活动中制作多个上下文菜单。有两个按钮,我想为每个按钮创建不同的上下文菜单。任何人都可以建议我或向我推荐一些工作示例吗?
这是我当前的代码的样子。
registerForContextMenu(btn_1);
registerForContextMenu(btn_2);
btn_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openContextMenu(v);
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openContextMenu(v);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.media_menu, menu);
menu.setHeaderTitle("Context menu for btn_1");
menu.setHeaderIcon(R.drawable.icon_media_up);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.photo:
Toast.makeText(this, "One", Toast.LENGTH_SHORT).show();
return true;
case R.id.video:
Toast.makeText(this, "Two", Toast.LENGTH_SHORT).show();
return true;
case R.id.audio:
Toast.makeText(this, "Three", Toast.LENGTH_SHORT).show();
return true;
}
return super.onContextItemSelected(item);
}