Use of Menu level-1 and level-2 menus in Android learning notes

Source: Internet
Author: User

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 );}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.