usually we see the application in the Chang ' an text or button, will pop-up menu, so select some menu items, how exactly is it implemented?
Today we will introduce this menu (context menu)
Creating a context menu requires only three steps:
1. Registerforcontextmenu (the control you want to register): register ContextMenu for the control.
2. Rewrite public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo Menuinfo): Here we are mainly to define the menu.
3. Rewrite public boolean oncontextitemselected (MenuItem Item): When we click on some menu items, we make some corresponding.
Here is an example:
main Activity code:
public class Mainactivity extends Activity {private TextView context_menu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); context_menu= ( TextView) Findviewbyid (r.id.context_menu);/* * Control Registration */registerforcontextmenu (CONTEXT_MENU);} @Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {//TODO auto-generated Method Stubsuper.oncreatecontextmenu (menu, V, menuinfo);/* Add menu item XML file */menuinflater inflater=new Menuinflater (This) ; Inflater.inflate (R.menu.menu, menu);} /* * ContextMenu Event Handling */@Overridepublic Boolean oncontextitemselected (MenuItem Item) {//TODO auto-generated method STUBSW Itch (Item.getitemid ()) {case R.id.menu1:toastmake (1); Break;case R.id.menu2:toastmake (2); Break;case R.ID.MENU3: Toastmake (3); Break;case R.id.menu4:toastmake (4); Break;case R.id.menu5:toastmake (5); break;default:break;} return super.oncontextitemselected (item);} /* * Test event */private void Toastmake (iNT Itemid) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, "You clicked on the menu item" +itemid, Toast.length_short). Show ();}}
Activity_layout.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:id=" @+id/context_menu " android:layout_centerinparent=" true " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" Press and hold these words " android:textsize=" 40SP "/></relativelayout>
Res/menu/menu.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu1" android:title= "Menu 1"/> <item android:id= "@+id/ Menu2 " android:title=" Menu 2 "/> <item android:id=" @+id/menu3 " android:title=" Menu 3 "/> <item android:id= "@+id/menu4" android:title= "Menu 4"/> <item android:id= "@+id/menu5" android:title= "Menu 5"/></menu>
:
Learn about Android Ax (5)