Android学习之menu
2010-05-31 14:13:00 来源:WEB开发网19. // The order position of the item
20. int menuItemOrder = Menu.NONE;
21.
22. menu.add(groupId, MENU_GROUPITEM1, menuItemOrder, "Item 1 in group");
23. menu.add(groupId, MENU_GROUPITEM2, menuItemOrder, "Item 2 in group")
24. .setChecked(true);
25. menu.setGroupCheckable(groupId, true, true); //这句要写在group item的最后
26.
27. SubMenu subMenu = menu.addSubMenu("Sub Menu 1");
28. subMenu.add("Sub Menu Item")
29. .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
30. @Override
31. public boolean onMenuItemClick(MenuItem item) {
32. Toast.makeText(HelloDemo.this,
33. "Sub Menu Item selected",
34. Toast.LENGTH_SHORT).show();
35. return true; //true表示完成当前item的click处理,不再传递到父类处理
36. }
37. });
38.
39. menu.add("Item 3").setCheckable(true).setChecked(true);
40. }
private static final int MENU_GROUPITEM1 = Menu.FIRST + 8; private static final int MENU_GROUPITEM2 = Menu.FIRST + 9; private static final int MENU_ITEM1 = Menu.FIRST + 10; public void onCreate(Bundle savedInstanceState) { ... registerForContextMenu(findViewById(R.id.edittext)); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(1,MENU_ITEM1,Menu.NONE, "Item 1").setCheckable(true).setChecked(false); // Group ID int groupId = 0; // The order position of the item int menuItemOrder = Menu.NONE; menu.add(groupId, MENU_GROUPITEM1, menuItemOrder, "Item 1 in group"); menu.add(groupId, MENU_GROUPITEM2, menuItemOrder, "Item 2 in group") .setChecked(true); menu.setGroupCheckable(groupId, true, true); //这句要写在group item的最后 SubMenu subMenu = menu.addSubMenu("Sub Menu 1"); subMenu.add("Sub Menu Item")
更多精彩
赞助商链接