Android's Optionsmenu

Source: Internet
Author: User
<span id="Label3"></p><p><p>Optionsmenu is a menu mode provided by android, we know that when the smart machine is emerging, the phone will have a menu (menu key), when we click, by default, we open the default menu provided by android, This article I would like to learn, How to customize the Android menu Menu.</p></p><p><p>When we create an activity, the default implementation of the OnCreate method, we want to implement the Android menu, we also need to implement the other two methods: oncreateoptionsmenu (); onoptionsitemselected (), Let's take a look at how to use it.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); } @Override</span></span><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;">oncreateoptionsmenu (menu menu) {menu.add (</span></span>0, 0, 0, "share"<span style="color: #000000;"><span style="color: #000000;">); Menu.add (</span></span>0, 1, 1, "about"<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;">; } @Override</span></span><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>0<span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">call to send SMS function</span></span>Intent intent=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (intent.action_send); Intent.settype (</span></span>"text/plain"<span style="color: #000000;"><span style="color: #000000;">); Intent.putextra (intent.extra_subject,</span></span>Share<span style="color: #000000;"><span style="color: #000000;">); Intent.putextra (intent.extra_text,</span></span>"I would like-to-share this ...");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">SMS Content</span></span><span style="color: #000000;"><span style="color: #000000;">startactivity (intent.createchooser (Intent, getTitle ())); </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>1<span style="color: #000000;"><span style="color: #000000;">: Toast.maketext (mainactivity.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, "i wish you a happy and enjoyable!" "<span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); </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;"></span> break</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><p><p>well, our Android menu is here, so let's try it.</p></p>

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.