"android first line code" learning record 02-use menu in activity

Source: Internet
Author: User
<span id="Label3"></p><p><p>Necessary Steps:</p></p> <ul> <ul> <li>Create a new menu folder in the Res directory and create a new menus file</li> <li>To add a menu item to it, like the following code:</li> </ul> </ul> <blockquote> <blockquote> <pre><menu xmlns:android= "http:<span style="color: #008000;">//</span><span style="color: #008000;">schema.android.com/apk/res/android" ></span> <<span style="color: #000000;">item Android:id</span>= "@+id/<span style="color: #000000;">itemId1" android:title</span>= "itemName1"/> <<span style="color: #000000;">item Android:id </span>= "@+id/<span style="color: #000000;">itemId2" android:title</span>= "itemName2"/></menu></pre> </blockquote> </blockquote> <ul> <ul> <li>Reload the Oncreateoptionsmenu () method in the activity using the menu to load the menu file, similar to the following code:</li> </ul> </ul> <blockquote> <blockquote> <pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Boolean</span> <span style="color: #000000;">oncreateoptionsmenu (menu menu) { getmenuinflater.inflate (r.menu); </span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span> <span style="color: #000000;">;}</span></pre> <p><span style="color: #000000; line-height: 1.5; background-color: initial;">Overload the Onoptionsitemselected () method in an activity that uses the menu to respond to the actions that are performed after each menu item click, similar to the following code:</span></p> </blockquote> </blockquote> <blockquote> <pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">onoptionsitemselected (MenuItem Item) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(item.getitemid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.itemid1:</span> ...</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Working with Code</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.itemid2:</span> ...</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Working with Code</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span></pre> </blockquote><p><p>"android first line code" learning record 02-use menu in activity</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.