Android context menu implementation, android context menu
1. overwrite the onCreateContenxtMenu () method of the Activity, and call the add method of Menu to add a Menu item (MenuItem ).
2. overwrite the onContextItemSelected () method of the Activity and respond to the Click Event of the context menu item.
3. Call the registerForContextMenu () method to register the context menu for the view.
JAVA code:
PackageEOE. local. contextMenu;
Importandroid. app. ListActivity;
Importandroid. OS. Bundle;
Importandroid. view. ContextMenu;
Importandroid. view. Menu;
Importandroid. view. MenuItem;
Importandroid. view. ContextMenu. ContextMenuInfo;
Importandroid. view. View;
Importandroid. widget. AdapterView. AdapterContextMenuInfo;
Importandroid. widget. ArrayAdapter;
Importandroid. util. Log;
PublicclassMainActivityextendsListActivity {
PrivatestaticfinalintITEM1 = Menu. FIRST;
PrivatestaticfinalintITEM2 = Menu. FIRST + 1;
PrivatestaticfinalintITEM3 = Menu. FIRST + 2;
/** Calledwhentheactivityisfirstcreated .*/
@ Override
Public void onCreate (BundlesavedInstanceState ){
Super. onCreate (savedInstanceState );
ShowListView ();
// Register ContextMenu for all items in ListView
RegisterForContextMenu (getListView ());
// The registerForContextMenu () here can also be replaced by the following statement
// GetListView (). setOnCreateContextMenuListener (this );
}
PrivatevoidshowListView (){
String [] mString = newString [] {
"Lufei-MonkeyDLuffy ",
"Naimei-Nami ",
"Zhuo lo-Zoro ",
"Shan zhi-Sanji ",
"Nicole Robin-Ms. AllSunday ",
"Usop-usoppu ",
"Tony Joba-TonyTonyChopper ",
};
ArrayAdapter <String> mla = newArrayAdapter <String> (MainActivity. this,
R. layout. main, mString );
MainActivity. this. setListAdapter (mla );
}
// Context menu. The context menu will be activated through long-pressed entries.
@ Override
PublicvoidonCreateContextMenu (ContextMenumenu, Viewview,
ContextMenuInfomenuInfo ){
Menu. setHeaderTitle ("Profile ");
// Add a menu item
Menu. add (0, ITEM1, 0, "special ");
Menu. add (0, ITEM2, 0, "Combat Capability ");
Menu. add (0, ITEM3, 0, "classic quotes ");
}
// Click response in the menu
@ Override
PublicbooleanonContextItemSelected (MenuItem item ){
// Obtain the information of the selected menu item
// AdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo ();
// Log. I ("braincol", String. valueOf (info. id ));
Switch (item. getItemId ()){
CaseITEM1:
// Add the processing code here
Break;
CaseITEM2:
// Add the processing code here
Break;
CaseITEM3:
// Add the processing code here
Break;
}
Returntrue;
}
}
Main. xml layout file:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<TextViewxmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/myTextView"
Android: textSize = "20sp"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "@ string/hello"
/>
Context menu cannot be implemented in android programming for beginners
Have you changed Menu/main. xml?
How to differentiate multiple context menus in android
Set a global variable. Set the global variable to the corresponding value on time.