The menu implementation method of Android programming _android

Source: Internet
Author: User

This article illustrates the menu implementation method of Android programming. Share to everyone for your reference, specific as follows:

Menus are an integral part of many applications, even more so in Android, where all mobile phones with Android have a "menu" button, which is the key to the menu, which shows how important and special the menus are in the Android program. The Android SDK offers three types: Options menu (General menu), context menu, and submenu (submenu). Where the Options menu is displayed by pressing the menu key, the context menu is displayed after 2 seconds on view (view Control), both of which can be nested with a child menu, but the submenu cannot be nested.

The Options menu can only display six menu options at the bottom of the screen, the six menus are called icon menu, icon menu can not have checkable option, more than 6 of the more icon menu to call, called expanded menu ( Extended menu). The Options menu is generated by the oncreateoptionsmenu of the activity, which is invoked only when the menu is first generated, and anything that wants to change the options Menu can only be implemented by Onprepareoptionsmenu, which is invoked before menu display. Onoptionsitemseleted used to process the selected options Menu menu item

The context menu is bundled with a specific view, which is used in an activity to register a view with a registerforcontextmenu. The context menu calls Oncreatecontextmenu before it is displayed to generate menu,oncontextitemseleted to process the selected context menu menu.

submenu is a submenu that can be nested in both of these menus, but a submenu cannot embed a submenu

Android also provides the ability to group menu items by grouping menus of similar functions in a group so that you can set the property menu by calling Setgroupcheckable,setgroupendable,setgroupvisible. Instead of setting it individually.

1, here is an example of creating the Options menu:

First create a menu folder in Res, where you can create a lot of menu resource files for easy management. Create an Android XML file in this folder with the following code:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <item
    android:id=" @+id/newfile "
    android:icon=" @drawable/ic_launcher "
    android: title= "@string/newfile_title"/> <item android:id= "@+id/ecit" android:icon= "
    @drawable/ic_ Launcher "
    android:title=" @string/exit_title "/>
</menu>

This creates two menu options, each item is a menu option, and the icon is not written; the contents of the title are written in a string file, which is the title of the menu. Finally, create the menu through the Oncreateoptionsmenu method in the activity, and then process the menu through the Onoptionsitemselect method. The code is as follows:

Package cn.csdn.android.menu;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuInflater;
Import Android.view.MenuItem;
Import Android.view.MenuItem.OnMenuItemClickListener;
Import Android.widget.Toast;
The public class Menutest1activity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '
---'
@Overrid E public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
@Override Public
Boolean oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.menu.options_menu, menu);
return true;
}


The results of the operation are shown in the following illustration:

You can see these two menus at the bottom of the screen.

2. Adding methods to the activity can respond to some of the events in the menu, by adding the Onoptionsitemselected method to its activity through the above example, which can be done by the menu. The code for onoptionsitemselected is as follows:

public boolean onoptionsitemselected (MenuItem item) {
 switch (Item.getitemid ()) {case
 r.id.newfile:
 Toast.maketext (This, "New file", Toast.length_long). Show ();
 break;
 Case R.ID.ECIT:
 //toast.maketext (This, "exit", Toast.length_long). Show ();
 This.finish ();
 break;


After adding this method, click the "Exit" button to exit and click "New file" to appear in the dialog box named "New FileName".
You can also work with the Onmenuitemclick method: The following code

public boolean Onmenuitemclick (MenuItem item) {
switch (Item.getitemid ()) {case
r.id.newfile:
Toast.maketext (This, "New file", Toast.length_long). Show ();
break;
Case R.ID.ECIT:
//Toast.maketext (here, exit, Toast.length_long). Show ();
This.finish ();
break;
return false;

function is the same as the previous method

3, create a submenu, insert the menu in the XML file, the following example is to insert two menus in the second menu: XML File source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <item
    android:id=" @+id/menuitem1 "
    android:title=" menuitem1
    "/> <item Android:id= "@+id/menuitem2"
    android:title= "menuitem2" >
    <menu >
      <item android:id=
        "@ +id/menuitem3 "
        android:title=" MenuItem3 "/>
      <item android:id=
        " @+id/menuitem4 "
        Android : title= "MENUITEM4" >
      </item>
    </menu>
  </item>
</menu>

4. A submenu with a single selection or check, add the Setgroupcheckable method when creating the menu to determine whether to add a radio or a check:

public boolean Oncreateoptionsmenu (Menu menu) {
getmenuinflater (). Inflate (R.menu.second_menu, menu);
submenu sm = Menu.finditem (r.id.item3). GetSubMenu ();
Sm.seticon (r.drawable.ic_launcher);
Sm.setgroupcheckable (0, True, true);
Sm.getitem (0). Setcheckable (true);
Sm.getitem (0). Setchecked (true);
This.menu = menu;
return Super.oncreateoptionsmenu (menu);


I hope this article will help you with the Android program.

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.