Android menu Analysis 01 (OptionsMenu)

Source: Internet
Author: User

Android menu Analysis 01 (OptionsMenu)

The menu mechanism of Android is quite different before and after Android 3.0. ActionBar is launched in Android 3.0, and the UI interaction of navigation is greatly changed, however, the menu logic is the same as that of the interface. Here we will introduce how to create and use Android menus. Next we will introduce how to use ActionBar, SherlockActionBar, and menus to create custom menus.

There are four main types of Android menus:

OptionMenu

ContextMenu

PopMenu

SubMenu

Next, we will introduce OptionMenu, which uses xml files and JAVA code to create menus.

1. OptionMenu

Option Menu. When you click the Menu key, the Menu is displayed. Select the Menu item or click another area. The Menu disappears.

The related methods of optionMenu are implemented in the Activity. The related methods include:

 

 

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    Log.d(tag, onCreateOptionsMenu);    return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onPrepareOptionsMenu(Menu menu) {    Log.d(tag, onPrepareOptionsMenu);    return super.onPrepareOptionsMenu(menu);}@Overridepublic void onOptionsMenuClosed(Menu menu) {    System.out.println(onOptionsMenuClosed);    Log.d(tag, onOptionsMenuClosed);    super.onOptionsMenuClosed(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    switch (item.getItemId()) {    case R.id.action_save:        Log.d(tag, action_save);        break;    case R.id.action_settings:        Log.d(tag, action_settings);        break;    case R.id.action_delete:        Log.d(tag, action_delete);        break;    case R.id.action_edit:        Log.d(tag, action_edit);        break;    }    return super.onOptionsItemSelected(item);}

 

OnCreateOptionMenu, within the Activity LifecycleRun only once, OnPrepareOptionMenuEach time the menu is openedThe onOptionsMenuClosed method is executed when the menu disappears, and the onOptionsItemSelected method is executed when the menu is selected.

The OptionMenu created with XML in Android 2.3 is as follows.


 

 

First: Four menu options without icons

Second: 8 menu items with icons

Third: Click More, no icon

The same Code, ifIncompatible (minimum version> 11)In versions earlier than Android 3.0, what is the effect on Android?


 

 

 

The first one has four menu options. There is no icon and only two items are displayed. There is one more icon.

Second: Click More menu items

Third: Eight menu items with icons, but the displayed menu has no text. More menu items only contain text

Figure 4: Click More with no icon

Next, let's take a look at the specific code.

1. Compile menu. xml


 

 

                 

 

Android: orderInCategory position of the menu item in the menu

 

2. Add a menu in Activity


 

 

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    Log.d(tag, onCreateOptionsMenu);    inflater.inflate(R.menu.main_icon, menu);    return super.onCreateOptionsMenu(menu);}

 

 

3. Menu Event Response


 

    @Overridepublic boolean onOptionsItemSelected(MenuItem item) {    switch (item.getItemId()) {    case R.id.action_save:        Log.d(tag, action_save);        break;    case R.id.action_settings:        Log.d(tag, action_settings);        break;    case R.id.action_delete:        Log.d(tag, action_delete);        break;    case R.id.action_edit:        Log.d(tag, action_edit);        break;    case R.id.action_search:        Log.d(tag, action_search);        break;    }    return super.onOptionsItemSelected(item);}

 

In this way, the menu creation, use, and response are completed.

Android officially recommends using XML to create menus. You can also create menus in JAVA code. The implementation process is as follows:

A. Add a menu item directly in the onCreateOptionsMenu () method.


 

@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {MenuInflater inflater = getMenuInflater (); // menu. add (int groupid, int itemid, int orderid, String title) method parameter // The add method returns a MenuItem object. You can add an icon and call the setIcon () method Log. d (tag, onCreateOptionsMenu); menu. add (0, Menu. FIRST + 1, Menu. FIRST + 1, menusStrings [0]). setIcon (android. r. drawable. ic_menu_add); menu. add (0, Menu. FIRST + 2, Menu. FIRST + 2, menusStrings [1]). setIcon (android. r. drawable. ic_menu_agenda); menu. add (0, Menu. FIRST + 3, Menu. FIRST + 3, menusStrings [2]). setIcon (android. r. drawable. ic_menu_call); menu. add (0, Menu. FIRST + 4, Menu. FIRST + 4, menusStrings [3]). setIcon (android. r. drawable. ic_menu_close_clear_cancel); menu. add (0, Menu. FIRST + 5, Menu. FIRST + 5, menusStrings [4]). setIcon (android. r. drawable. ic_menu_crop); menu. add (0, Menu. FIRST + 6, Menu. FIRST + 6, menusStrings [5]). setIcon (android. r. drawable. ic_menu_upload); inflater. inflate (R. menu. main, menu); return super. onCreateOptionsMenu (menu );}

 

B. Respond to the selected event in the menu

The method for responding to selected events is the same as above, so the code will not be pasted ~.

 

That's all about OptionsMenu. If you have any questions, please comment.

 

 

 

 

 

 

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.