"go" android oncreateoptionsmenu () create menu details

Source: Internet
Author: User
<span id="Label3"></p><p><p>Original Url: http://www.linuxidc.com/Linux/2012-02/55500.htm</p></p><p>Android has three different forms of menus:<br>1. Options Menu (optinosmenu)<br>2. context Menu (ContextMenu)<br>3. sub-menu (submenu)<br>The most common of these is the Options menu (optionsmenu), which is displayed at the bottom of the corresponding activity when you click the menu Button.<br><br><strong><strong>1.Activity menu Mechanism</strong></strong>(similar to Dialog)<br>Activity has a mechanism for managing menus, as Follows:<br>1.public Boolean Oncreateoptionsmenu (menu Menu)<br><span><span><code>             此方法用于初始化菜单,其中menu参数就是即将要显示的Menu实例。<br>              返回true则显示该menu,false 则不显示;<br>              </code><strong><code>(只会在第一次初始化菜单时调用)</code></strong><br></span></span><code><code><br><span>           2.public boolean </span></code></code><code><code><span>onPrepareOptionsMenu(Menu menu)<br>             在</span></code></code>Oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is Displayed.<br>The same,<code><code><span> 返回true则显示该menu,false 则不显示;<br>             </span><strong>(可以通过此方法动态的改变菜单的状态,比如加载不同的菜单等)<br><br></strong>           3.public void onOptionsMenuClosed(Menu menu)<br>              每次菜单被关闭时调用.<br>              <strong>(</strong></code></code><strong><strong>The menu is closed in three situations where menu button is clicked again, the back button is clicked, or the user selects one of the Menus.</strong></strong><br><br>4.public Boolean onoptionsitemselected (MenuItem Item)<br>The menu item is invoked when clicked, that is, the method of listening for the food Item.<br><br>With these methods, it can be learned that for activity, only one menu object can be displayed and monitored at the same time.<br><br><strong><strong><br>2. Add a menu:</strong></strong><br><strong><strong>can be in Oncreateoptionsmenu or</strong></strong><code><code><span><strong>onPrepareOptionsMenu方法中来添加菜单</strong><br><br></span></code></code><strong><strong>2.1 Code Additions:</strong></strong><br>Menu.add (int groupId, int itemId, int order, charsequence title). setIcon (drawable ID)</p><p><p>The four parameters of the Add () method are, in Turn:</p></p><p><p>1, groups, If not grouped, then write menu.none,</p></p><p><p>2, id, This is very important, Android based on this ID to determine the different menu</p></p><p><p>3, order, which menu item in front by the size of this parameter is determined</p></p><p><p>4, text, menu item display text</p></p><p>The Add () method returns a MenuItem object, calls its SetIcon () method, sets the icon for the corresponding MenuItem<br><br>Example:<br>public boolean Oncreateoptionsmenu (menu Menu) {<br>Super.oncreateoptionsmenu (menu);<br>Menu.add (menu.none,<strong><strong>menu.first+1</strong></strong>, 0, "settings"). SetIcon (r.drawable.setting);<br>Return true;<br>}<br> <br><strong><strong>2.2 Layout file Additions:</strong></strong><br><br>Getmenuinflater (). Inflate (r.menu.options_menu, menu);<br><br>Call Activity's Getmenuinflater () to get a menuinflater,<br>Use the Inflate method to load the defined menu in the Layout file to the menu object corresponding to the second parameter<br><br>Example:<br>@Override<br>public boolean Oncreateoptionsmenu (menu Menu) {<br>Super.oncreateoptionsmenu (menu);<br>Getmenuinflater (). Inflate (<strong><strong>R.menu.options_menu</strong></strong>, menu);<br>Return true;<br>}<br><br>Layout file:<br>Set up a menu folder in the Res directory and create a layout file: options_menu.xml<br><?xml version= "1.0" encoding= "utf-8"?><br><menu xmlns:android= "http://schemas.android.com/apk/res/android" ><br><item android:id= "<strong><strong>@+id/menu_setting</strong></strong>"android:title=" setting "android:icon=" @drawable/setting "></item><br></menu><br><br><br><strong><strong>3. menu item Monitoring:</strong></strong><br>Onoptionsitemselected (MenuItem Item) is triggered whenever a menu item in the menu is clicked<br>The item parameter is the menu item that is clicked, so it is necessary to determine which item is clicked in this method, thus implementing different actions.<br>For the two different ways to add a menu, the method of judging is a little different, but the essence is the Same.<br><br><strong><strong>3.1 How to determine the code to add a menu</strong></strong><br>@Override<br>public boolean onoptionsitemselected (MenuItem Item) {<br>Super.onoptionsitemselected (item);<br>Switch (item.getitemid ())//get clicked on the Item's itemid<br>{<br>Case<strong><strong>menu.first+1</strong></strong>://ID is the ID set in the Add method<br>Break<br>Case Menu.first+2:<br>Break<br>}<br>Return true;<br>}<br><br><strong><strong>3.2 How to determine the layout file add menu:</strong></strong><br>@Override<br>public boolean onoptionsitemselected (MenuItem Item) {<br>Super.onoptionsitemselected (item);<br>Switch (item.getitemid ())//get clicked on the Item's itemid<br>{<br>Case<strong><strong>r.id.menu_setting</strong></strong>://the ID here is the ID defined in the layout file, which is obtained by means of r.id.xxx<br>Break<br>Case R.id.menu_info:<br>Break<br>}<br>Return true;<br>}</p><p><p>"go" android oncreateoptionsmenu () create menu details</p></p></span>

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.