Based on the Android implementation click a button to let the menu options from the button around the location pop-up _android

Source: Internet
Author: User

Android Material Design:popupmenu

The Android Material Design introduces a popupmenu similar to the context menu in the past, but more flexible. As shown in the figure:

Now give the code to implement the figure PopupMenu. This example is an ordinary button-triggered pop-up popupmenu.

Mainactivity.java of the test:

Package zhangphil.materialdesign; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.support.v.widget.popupmenu; 
Import Android.view.MenuItem; 
Import Android.view.View; 
Import Android.widget.Button;  
Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Supe 
    R.oncreate (savedinstancestate);  
    Setcontentview (R.layout.activity_main); 
    Final button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Show 
      PopupMenu (button); 
  } 
    }); } private void ShowPopupMenu (view view) {//view the position of the relative View displayed by the current popupmenu popupmenu popupmenu = new PopupMenu  
    (this, view);  
    Menu Layout Popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ()); menu item Click event Popupmenu.setonmenuitemclicklistener (New Popupmenu.onmenUitemclicklistener () {@Override public boolean Onmenuitemclick (MenuItem item) {Toast.maketext 
        ApplicationContext (), Item.gettitle (), Toast.length_short). Show (); 
      return false;  
     } 
    }); PopupMenu Shutdown Event Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {@Override public void Ondismiss (PopupMenu menu) {Toast.maketext (Getapplicationcontext (), close PopupMenu, Toast.length_short). Show 
      (); 
  
     } 
     }); 
   Popupmenu.show ();
}} package zhangphil.materialdesign;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v7.widget.PopupMenu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.oncre
 Ate (savedinstancestate);
 Setcontentview (R.layout.activity_main); Final button button = (Button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {showpopupmenu (button)
  ;
 }
 }); } private void ShowPopupMenu (view view) {//view the position of the relative View displayed by the current popupmenu popupmenu popupmenu = new PopupMenu (This, view
 );
 Menu Layout Popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ()); menu item Click event Popupmenu.setonmenuitemclicklistener (New Popupmenu.onmenuitemclicklistener () {@Override public bool EAN Onmenuitemclick (MenuItem Item) {Toast.maketext (Getapplicationcontext (), Item.gettitle (), Toast.length_short).
  Show ();
  return false;
 }
 }); PopupMenu Shutdown Event Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {@Override public void Ondismiss P
  Opupmenu menu) {Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();
 }
 });
 Popupmenu.show (); }
}

The Activity_main layout file that Mainactivity.java needs contains only one ordinary Android Button, which is not included in the time.

Main.xml in the menu directory:

<menu xmlns:android= "http://schemas.android.com/apk/res/android" 
   xmlns:app= "http://schemas.android.com/ Apk/res-auto " 
   xmlns:tools=" Http://schemas.android.com/tools "> 
   <item 
     android:id=" @+id/action_ Open " 
     android:orderincategory=" 
     android:title= " 
     app:showasaction=" never/> 
  <item 
    android:id= "@+id/action_new" 
    android:orderincategory= " 
    android:title=" new 
    app: showasaction= "Never"/> 
  <item 
    android:id= 
    "@+id/action_del" android:title= "Delete" 
     app:showasaction= "never"/> 
 

The above content for you to share on the Android implementation click on a button to let the menu options from the button around the designated location pop-up knowledge, I hope you like.

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.