Android implementation is similar to the right button pop-up menu effect in PCs _android

Source: Internet
Author: User

The ContextMenu (context menu) in the Android system is similar to a right-click pop-up menu in a PC, and when a view is registered to a context menu, a "long press" action on the object appears, and a floating menu is available that provides the relevant functionality. Context menus can be registered in any view object, but the most common is the item used for List view ListView, when the list item is pressed, the background color is converted and the context menu is prompted to render it.
Note: the context menu does not support icons and shortcut keys.
In order to create a context menu, you must override the context menu callback function for this activity: Oncreatecontextmenu () and oncontextitemselected (). In the callback function Oncreatecontextmenu (), you can add a menu item by using an Add () method, or expand a menu resource defined in XML. Then, register a context menu ContextMenu for this view through Registerforcontextmenu ().
Below I have an example demo to show the basic use of ContextMenu, by manually adding menu menu and XML file two ways to configure menu options.
1, the demo of the engineering structure of the directory map as follows:

2, the Res/layout directory under the main.xml layout file source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:orientation=" vertical " 
 android:layout_width=" fill_parent " 
 android:layout_" height= "Fill_parent" > 
 <textview 
  android:layout_width= "Fill_parent" 
  Wrap_content " 
  android:text=" Hello,this is Andy ' s blog! " /> 
 <listview 
  android:id= "@+id/lv" 
  android:layout_width= "fill_parent" 
  android:layout_ height= "Wrap_content"/> 

3, Res/menu directory under the Cmenu.xml menu options File source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android= 
 "http://schemas.android.com/apk/res/" Android "> 
 <item android:id=" @+id/add "android:title=" Add "/> <item android:id=" @+id/update " 
 android:title= "Update"/> 
 <item android:id= "@+id/delete" android:title= "delete"/> 
</menu> 

4, Com.andyidea.menudemo package under the Menuactivity.java source code as follows:

Package Com.andyidea.menudemo; 
Import java.util.ArrayList; 
 
Import java.util.List; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.ContextMenu; 
Import Android.view.ContextMenu.ContextMenuInfo; 
Import Android.view.Menu; 
Import Android.view.MenuInflater; 
Import Android.view.MenuItem; 
Import Android.view.View; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.ListView; 
 
Import Android.widget.Toast; 
 public class Menuactivity extends activity {ListView LV; 
 Private arrayadapter<string> adapter; 
  
 Private list<string> alist = new arraylist<string> (); /** called the activity is a. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
   
  Setcontentview (R.layout.main); 
  LV = (ListView) Findviewbyid (r.id.lv); 
  Alist.add ("first"); 
  Alist.add ("second"); 
  Alist.add ("third"); adapter = new Arrayadapter<string> (this,android. R.layout.simple_Expandable_list_item_1,alist); 
   
  Lv.setadapter (adapter); 
 Registers the View object, which is the ListView control Registration context Menu Registerforcontextmenu (LV);  /** * Create context menu option * * @Override public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo 
  Menuinfo) {//1. Configure context menu options by adding them manually//menu.add (0, 1, 0, "modify"); 
   
  Menu.add (0, 2, 0, "delete"); 
  2. Configure context menu options through an XML file Menuinflater Minflater = Getmenuinflater (); 
   
  Minflater.inflate (R.menu.cmenu, menu); 
 Super.oncreatecontextmenu (menu, V, menuinfo); /** * Call this method when a menu option is clicked * * @Override public boolean oncontextitemselected (MenuItem item) {Switch (ITEM.G 
   Etitemid ()) {case 1:toast.maketext (this, "You have selected manual modification", Toast.length_short). Show (); 
  Break 
   Case 2:toast.maketext (This, "you chose to delete manually", Toast.length_short). Show (); 
  Break 
   Case R.id.add:toast.maketext (This, "You have selected XML additions", Toast.length_short). Show (); 
  Break Case R.id.update:toast.maketext (This, "you selected XML Update", toast.length_sHort). Show (); 
  Break 
   Case R.id.delete:toast.maketext (This, "You chose XML deletion", Toast.length_short). Show (); 
  Break 
 return super.oncontextitemselected (item); /** * The method called when the context menu is closed */@Override public void oncontextmenuclosed (Menu menu) {//TODO auto-generated m 
 Ethod stub super.oncontextmenuclosed (menu); 
 } 
  
}

5, the demo run after the operation screenshot as follows:

I hope this article will help you learn about Android software programming.

Related Article

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.