PopupMenu-----Popup Menu

Source: Internet
Author: User

1 ImportAndroid.os.Bundle;2 Importandroid.app.Activity;3 ImportAndroid.graphics.Color;4 ImportAndroid.view.Menu;5 ImportAndroid.view.MenuItem;6 ImportAndroid.view.View;7 ImportAndroid.widget.PopupMenu;8 ImportAndroid.widget.PopupMenu.OnMenuItemClickListener;9 ImportAndroid.widget.TextView;Ten  One  Public classMainactivityextendsActivity { A  -     PrivateTextView TextView; -     Private floatFontSize =15; the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +          -TextView =(TextView) Findviewbyid (R.id.textview); +     } A     //The popup menu starts at 3.0, so the API is the lowest at      Public voidBtnclick (View v) -     { -         //The second parameter is the reference position of the pop-up menu display, which is usually displayed below the reference view -         //Create a popup menu object -PopupMenu pop =NewPopupMenu ( This, v); -          in         //to add a menu item to a menu - getmenuinflater (). Inflate (R.menu.main, Pop.getmenu ()); to          +         //adding event listeners to a menu item -Pop.setonmenuitemclicklistener (NewOnmenuitemclicklistener () { the              * @Override $              Public BooleanOnmenuitemclick (MenuItem item) {Panax Notoginseng                 Switch(Item.getitemid ()) -                 { the                      CaseR.id.action_add: +Fontsize+=5; A textview.settextsize (fontSize); the                          Break; +                      Caser.id.action_sub: -Fontsize-=5; $ textview.settextsize (fontSize); $                          Break; -                      CaseR.id.action_background: -                         intRed = (int) (Math.random () *256); the                         intGreen = (int) (Math.random () *256); -                         intBlue = (int) (Math.random () *256);Wuyi Textview.setbackgroundcolor (Color.rgb (red, green, blue)); the                          Break; -                 } Wu                 return false; -             } About         }); $          -         //Show Menu - pop.show (); -     } A  +}
mainactivity
1<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >2 3<Item4Android:id= "@+id/action_add"5android:orderincategory= "100"6Android:showasaction= "Never"7android:title= "Enlarge font"/>8<Item9Android:id= "@+id/action_sub"Tenandroid:orderincategory= "100" OneAndroid:showasaction= "Never" Aandroid:title= "Shrink Font"/> -<Item -Android:id= "@+id/action_background" theandroid:orderincategory= "100" -Android:showasaction= "Never" -android:title= "Change background color"/> -  +</menu>
Menu_main

PopupMenu-----Popup Menu

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.