Android context menu implementation, android context menu

Source: Internet
Author: User

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.

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.