Menu creation menu, Menu Creation

Source: Internet
Author: User

Menu creation menu, Menu Creation

Menus are one of the most common elements in the user interface and are frequently used. In Android, menus are divided into three types: OptionsMenu and ContextMenu) and SubMenu. Today it is OptionsMenu.

I. Overview: I am lazy here. I quoted it from the Internet, not original. Reference: ForrestWoo

  Public boolean onCreateOptionsMenu (Menu menu): use this method to call OptionsMenu.

Public boolean onOptionsItemSelected (MenuItem item): The action that occurs after the menu item is selected.

Public void onOptionsMenuClosed (Menu menu): The action that occurs after the Menu is closed.

Public boolean onPrepareOptionsMenu (Menu menu): The onPrepareOptionsMenu method is called before the option Menu is displayed. You can use this method to adjust the menu according to the current situation.

Public boolean onMenuOpened (int featureId, Menu menu): The action that occurs after a single open.

First, create a menu file. Here android studio is used. The creation method is as follows:

1.

2.

3. After the creation is complete, write the following code in muen. xml to create two meun controls (C # is called a control, which is called here). The Code is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <menu xmlns: android = "http://schemas.android.com/apk/res/android"> 3 <item android: id = "@ + id/add_item" 4 android: title = "add a menu"/> 5 6 <item android: id = "@ + id/move_item" 7 android: title = "remove a menu" 8/> 9 </menu>

It is displayed in Studio as follows:

4. Next we will rewrite the OnCrateOptionsMenu () method: the code is as follows:

1 /*@ onCreateOptionsMenu2 * */3     public boolean onCreateOptionsMenu(Menu menu) {4         getMenuInflater().inflate(R.menu.menu, menu);5         return true    ;6     }
View Code

* You can use the getMenuInflater () method to obtain the object and call inflate () to create an activity menu. It is not enough to display a menu. Add the following code:

 

1 public boolean onOptionsItemSelected (MenuItem item) 2 {3 switch (item. getItemId () 4 {5 case R. id. add_item: // ID 6 Toast defined in the menu file. makeText (this, "Pop Up A menu", Toast. LENGTH_SHORT ). show (); 7 break; 8 case R. id. move_item: // same as above 9 Toast. makeText (this, "close a menu", Toast. LENGTH_SHORT ). show (); 10 default: 11 12 break; 13} 14 return true; 15}
OnOptionsItemSelected

Override the onOptionsItemSelected method and call the getItemId () method to determine which menu is clicked. The effect is as follows:

 

 

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.