我正在为各种类型的音乐文件操作做一个上下文菜单。但是我无法选择上下文菜单项。在下面的代码中test4contextmenu
是一个包含TextView
的类布局。
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String menuItemName = menuItems[menuItemIndex];
String songName = songs.get(info.position);
text = (TextView)findViewById(R.id.text1);
text.setText(String.format("Selected %s for item %s", menuItemName, songName));
startActivity(new Intent(this,test4contextmenu.class));
return true;
}
答案 0 :(得分:0)
private void CreateMenu(Menu menu)
{
MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”);
{
mnu1.setAlphabeticShortcut(‘a’);
mnu1.setIcon(R.drawable.icon);
}
MenuItem mnu2 = menu.add(0, 1, 1, “Item 2”);
{
mnu2.setAlphabeticShortcut(‘b’);
mnu2.setIcon(R.drawable.icon);
}......
.......
........
........
private boolean MenuChoice(MenuItem item)
{
switch (item.getItemId()) {
case 0:
Toast.makeText(this, “You clicked on Item 1”,
Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, “You clicked on Item 2”,
Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, “You clicked on Item 3”,
Toast.LENGTH_LONG).show();
return true;
}
...........
.............
...............
return false;
}