Use of Menu level-1 and level-2 menus in Android learning notes
(1) No changes have been made to the layout file.
(2) the code in the main. xml file under the res -- menu directory is as follows:
VcGVu ">
(3) In the string. xml file under the values directory:
Menu_submenu
Settings
Hello world!
File
New
Open
Option
(3) The MainActivity. java code is as follows:
Package com. example. menu_submenu; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/** processing level-1 menu */@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. file: Toast. makeText (MainActivity. this, "Level 1 menu file .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. chioce: Toast. makeText (MainActivity. this, "Level 1 menu chioce .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item);}/** process level-2 menu */@ Overridepublic boolean onMenuItemSelected (int featureId, MenuItem item) {switch (item. getItemId () {case R. id. create1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. create: Toast. makeText (MainActivity. this, "Process Level 2 menu create .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open: Toast. makeText (MainActivity. this, "Process Level 2 menu open .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onMenuItemSelected (featureId, item );}}