Two menu creation methods under anroid

Source: Internet
Author: User

The Application menu is another important component of the application user interface. Menus provide a reliable interface for displaying application functions and settings. Press the menu key on the device to bring up the most common application menu. However, you can also add context menus that are called up when you long press a project.

Menus are structured by view layers, but you do not have to define the architecture yourself. You only need to define the oncreateoptionsmenu () and oncreatecontextmenu () callback methods for your activity, and declare the code of the transaction or action you want to include in the menu. Android will automatically create a view level for your menu and draw your menu items in it.
Menus process their events by themselves, so you do not have to register an event listener for your project. When an item in your menu is selected, the frame uses the onoptionsitemselected () or oncontextitemselected () method for automatic calling. Just like the application layout. You can also define projects in your menu in an XML file.

 

First, use menuinflater

Menuinflater is used to parse the menu layout file defined in the menu directory. menuinflater is used to generate menus. You will find out how nice it is, is to define Four menus and implement a menu event. Click the setting menu to enter the mobile phone setting status! Next let's take a look (the essence is to use an XML file to generate a menu ):

The following describes how to implement the demo: 1. Create an android project and name it menuinflaterdemo. 2. Create the menu directory under the res directory and create the options_menu.xml (the menu we define) file. The Code is as follows:
 
 
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <menu     
  4.   xmlns:android="http://schemas.android.com/apk/res/android">     
  5.     <item android:id="@+id/menu_add"    
  6.           android:title="Add"    
  7.           android:icon="@android:drawable/ic_menu_add"    
  8.            />     
  9.     <item android:id="@+id/menu_wallaper"    
  10.           android:title="Wallpaper"    
  11.           android:icon="@android:drawable/ic_menu_gallery"    
  12.            />     
  13.    <item android:id="@+id/menu_search"    
  14.           android:title="Search"    
  15.           android:icon="@android:drawable/ic_search_category_default"    
  16.            />     
  17.    <item android:id="@+id/menu_setting"    
  18.           android:title="Settings"    
  19.           android:icon="@android:drawable/ic_menu_preferences"    
  20.            />     
  21. </menu>    
  22. <?xml version="1.0" encoding="utf-8"?> 
  23. <menu 
  24.   xmlns:android="http://schemas.android.com/apk/res/android"> 
  25.     <item android:id="@+id/menu_add" 
  26.           android:title="Add" 
  27.           android:icon="@android:drawable/ic_menu_add" 
  28.            /> 
  29.     <item android:id="@+id/menu_wallaper" 
  30.           android:title="Wallpaper" 
  31.           android:icon="@android:drawable/ic_menu_gallery" 
  32.            /> 
  33.    <item android:id="@+id/menu_search" 
  34.           android:title="Search" 
  35.           android:icon="@android:drawable/ic_search_category_default" 
  36.            /> 
  37.    <item android:id="@+id/menu_setting" 
  38.           android:title="Settings" 
  39.           android:icon="@android:drawable/ic_menu_preferences" 
  40.            /> 
  41. </menu> 
Iii. Main class menuinflaterdemo. Java code. Little code is written here. I only wrote the RESPONSE event of the fourth menu (settings) here. All the code is as follows:
 
 
  1. View plaincopy to clipboardprint?
  2. Package com. Android. tutor;
  3. Import Android. App. activity;
  4. Import Android. content. intent;
  5. Import Android. OS. Bundle;
  6. Import Android. View. Menu;
  7. Import Android. View. menuinflater;
  8. Import Android. View. menuitem;
  9. Public class menuinflaterdemo extends activity {
  10. @ Override
  11. Public void oncreate (bundle savedinstancestate ){
  12. Super. oncreate (savedinstancestate );
  13. Setcontentview (R. layout. Main );
  14. }
  15. @ Override
  16. Public Boolean oncreateoptionsmenu (menu ){
  17. Menuinflater Inflater = getmenuinflater ();
  18. Inflater. Inflate (R. Menu. options_menu, menu );
  19. Return true;
  20. }
  21. @ Override
  22. Public Boolean onoptionsitemselected (menuitem item ){
  23. Switch (item. getitemid ()){
  24. Case R. Id. menu_add:
  25. Break;
  26. Case R. Id. menu_wallaper:
  27. Break;
  28. Case R. Id. menu_search:
  29. Break;
  30. Case R. Id. menu_setting:
  31. Showsettings ();
  32. Break;
  33. }
  34. Return super. onoptionsitemselected (item );
  35. }
  36. Private void showsettings (){
  37. Final intent settings = new intent (Android. provider. settings. action_settings );
  38. Settings. setflags (intent. flag_activity_new_task |
  39. Intent. flag_activity_reset_task_if_needed );
  40. Startactivity (settings );
  41. }
  42. }
Method 2: Use code to dynamically implement menu, mainly the menu. Add Method
1. After the program starts running, press menu to display the option 1 in the figure below:
2. If the "add item" option is selected, insert is displayed in the title area of the screen.
3. Press menu to display the menu. If del item is selected, delete is displayed in the title and title area.
4. Press menu to display the menu. If the exit option is selected, the program is finished.
// ---- Ex01.java program example ----
Package com. misoo. ex01;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;

Public class ex01 extends activity {
Public static final int add_id = menu. first;
Public static final int delete_id = menu. First + 1;
Public static final int exit_id = menu. First + 2;

@ Override public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );
}
@ Override public Boolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
Menu. Add (0, add_id, 0, R. String. menu_add );
Menu. Add (0, delete_id, 1, R. String. menu_delete );
Menu. Add (0, exit_id, 2, R. String. menu_exit );
Return true ;}
@ Override public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case add_id: settitle ("insert ...");
Break;
Case delete_id: settitle ("delete ...");
Break;
Case exit_id:
Finish ();
Break;
}
Return super. onoptionsitemselected (item );
}}
Step-3: change the content of/RES/values/strings. XML:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "app_name"> ex01 </string>
<String name = "menu_add"> Add item </string>
<String name = "menu_delete"> Del item </string>
<String name = "menu_exit"> exit </string>
</Resources>
And save it.
Step-4: Skip.

Two menu creation methods are used to create a two-level menu:
Shows how to add a menu for an application through Java code or XML. The menu is divided into two levels, level 1 menu items include "start", "cancel", and "about ...... "" about ...... "There are two sub-Menus" Help ...... "" Contact Us ......"
,,. The first menu is generated by Java code, and the sub menu is defined by XML. To define a menu item through XML, you need to create a file named menu. xml under the Res/menu directory to define the menu item. The specific process is as follows:

Create a project. Project name: "menudemo", application name: "menu example"
Create a file named menu. XML in the Res/menu/directory. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Menu
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item
Android: Id = "@ + ID/help"
Android: Title = "help"/>
<Item
Android: Id = "@ + ID/our"
Android: Title = "Contact Us"/>
</Menu>
Note:
This file defines a group of two menu items.
Compile menuactivity. Java
Package com. sharpandroid. Menu;
Import
Import
Import
Import
Import
Android. App. activity;
Android. OS. Bundle;
Android. View. Menu;
Android. View. menuinflater;
Android. View. menuitem;
Public class menuactivity extends activity {
Private Static final int OK = 1;
Private Static final int cancle = 2;
Private Static final int about = 3;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
@ Override
// When the menu key is clicked, the menu is opened. When the menu is started for the first time, the Framework calls back this method.
Public Boolean oncreateoptionsmenu (menu ){
// Add an element with ID 1 and title "start" to the menu
Menu. Add (0, OK, 0, "Start ");
Menu. Add (0, cancle, 0, "cancel ");
// Add a sub-menu for the menu. The ID is 3, the title is "about", and the returned sub-menu object is file.
Menu file = menu. addsubmenu (0, about, 0, "about ");
// Obtain a menuinflater object
Menuinflater Inflater = getmenuinflater ();
// Call the inflate method of Inflater to obtain the elements defined in the resource file,
// Add these elements to the specified menu -- File
Inflater. Inflate (R. Menu. submenubyxml, file );
Return true;
}
@ Override
// When a menu item is selected, the Framework calls back this method and passes in the clicked item.
Public Boolean onoptionsitemselected (menuitem item ){
// Perform different processing based on the selected item
Switch (item. getitemid ()){
Case OK:
This. settitle ("START ");
Return true;
Case cancle:
This. settitle ("cancel ");
Return true;
Case about:
This. settitle ("about ");
Return true;
Case R. Id. Help:
This. settitle ("help information ");
Return true;
Case R. Id. Our:
This. settitle ("Contact Us ");
Return true;
}
Return false;
}
}
Note:
This example demonstrates how to create a menu through Java code, add a sub-menu for it, and how to use XML
File definition menu items, and obtain information in Java code, and then add it to the specified menu.
Combined with metrics
Related Article

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.