Android menu event, androidmenu
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: finish(); break; case 2: break; } return super.onOptionsItemSelected(item); }
Android native development, how can we set ContextMenu to a non-Long-press event, with code attached
Public void onClick (View v) {super. onClick (v); this. openContextMenu (v );}
How can onOptionsItemSelected of android menu be associated with menu?
You can see that you created the menu in the xml format. In onOptionsItemSelected, switch {case (R. id. XXX)} corresponds to the click events of each menu. If it is code creation, it is the id you created.
For example, menu. add (0, 1, 1, "XXX"); this is the same in onOptionsItemSelected switch {
Case 1: // TODO (this is the click event of this menu item .) }
The menu method has been encapsulated in the parent class of Activity. You only need to override the onCreateOptionMenu () method of the parent class and onOptionsItemSelected.