Menu (Fri) Actionmode and PopupMenu

Source: Internet
Author: User

Package Com.example.actionmode;import Android.app.activity;import Android.os.bundle;import Android.view.ActionMode ; Import Android.view.menu;import android.view.menuinflater;import android.view.menuitem;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onlongclicklistener;import Android.widget.button;import Android.widget.popupmenu;import Android.widget.PopupMenu.OnMenuItemClickListener; Import Android.widget.toast;public class Mainactivity extends Activity {private button button1;private button button2; Private Actionmode actionmode; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid (r.id.button1); button2 = (Button) Findviewbyid (R.id.button2), Button1.setonlongclicklistener (new Onlongclicklistener () {@Overridepublic Boolean Onlongclick (View v) {//TODO auto-generated method stubif (Actionmode! = null) {return false;} Actionmode = StartaCtionmode (Actioncallback); v.setselected (true); return true;}}); Button2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubpopupmenu PopupMenu = new PopupMenu (mainactivity.this, v);p Opupmenu.setonmenuitemclicklistener (new Onmenuitemclicklistener () {@Overridepublic Boolean Onmenuitemclick (MenuItem Item) {//TODO auto-generated method Stubswitch (Item.getitemid ()) {case r.id.edit:edit (); break;case r.id.share:share (); break;case r.id.delete:delete (); Break;default:break;} return false;}}); Menuinflater menuinflater = Getmenuinflater (); Menuinflater.inflate (R.menu.main, Popupmenu.getmenu ()); Popupmenu.show ();//If there is space, it appears on the top of the view, no space appears below the View}}); Private Actionmode.callback Actioncallback = new Actionmode.callback () {@Overridepublic Boolean onprepareactionmode ( Actionmode mode, Menu menu) {//TODO auto-generated method Stubreturn false;} @Overridepublic void Ondestroyactionmode (Actionmode mode) {//TODO auto-generated method Stubactionmode = null;} Load menu from XML @overridepublic boolean oncreateactionmode (Actionmode mode, menu menu) {//TODO auto-generated method Stubmenu Inflater menuinflater = Getmenuinflater (); Menuinflater.inflate (R.menu.main, menu); return true; Note here to return true} @Overridepublic Boolean onactionitemclicked (actionmode mode, MenuItem Item) {//TODO auto-generated Method Stubswitch (Item.getitemid ()) {case r.id.edit:edit (); break;case r.id.share:share (); Break;case R.id.delete: Delete (); break;default:break;} return false;}}; public void edit () {Toast.maketext (mainactivity.this, "edit", Toast.length_short). Show (); public void Share () {Toast.maketext (Mainactivity.this, "share", Toast.length_short). Show (); public void Delete () {Toast.maketext (mainactivity.this, "delete", Toast.length_short). Show ();}}

Res/menu/main.xml

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/edit"        android:orderincategory= "android:showasaction="        never "        android:title=" edit/>    <item        android:id= "@+id/share"        android:orderincategory= "        android:showasaction=" never "        android: title= "Share"/>    <item        android:id= "@+id/delete"        android:orderincategory= "Android"        : Showasaction= "Never"        android:title= "Delete"/></menu>

Source code Download

Menu (Fri) Actionmode and PopupMenu

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.