Android menu analysis 02 (ContextMenu), androidcontextmenu

Source: Internet
Author: User

Android menu analysis 02 (ContextMenu), androidcontextmenu

In the previous articleAndroid menu Analysis 01 (OptionsMenu)This section introduces the use of OptionsMenu.ContextMenu.

ContextMenuContext menu for short. Two response modes are available through long-pressed event response.

  1. Floating Mode
    Similar to the pop-up Dialog. In the center of the screen, you can customize the displayed Menu and MenuItem response.
  2. Action Mode
    Implemented through ActionBar, where the effect is actually Title, an action bar appears.

Special note

ContextMenuAction ModeVersions earlier than Android 3.0 cannot be implemented (the action mode requires ActionBar support). If the application SherlockActionBar does not support verification.

Let's take a look at it first.Action ModeOf


Csdn cannot upload pictures.


The first figure shows the effect after a long press, and the title of the App changes to ActionMode. We can see that our MenuItem has only icons and no text.

Figure 2: After clicking more, there is no MenuItem displayed, only text, no icon.

Let's look at the ContextMenuFloating Mode


Csdn cannot upload pictures.


Long press Btn to display the page. Click MenuItem or an area outside the menu to disappear.

In earlier versions, ContextMenu has the same floating effect before Android 3.0, as shown in.

Now let's take a look at how to create a ContextMenu and call methods.

I. Action Mode

Create a ContextMenu Action Mode

  1. Register Action Mode
// Register the action mode registerForContextMenu (btnView) for Btn; // The response duration is based on the event btn. setOnLongClickListener (new OnLongClickListener () {@ Override public boolean onLongClick (View v) {v. setSelected (true); ContextMenu. this. startActionMode (mActionMode); return true ;}});

2. Implement the Action Mode callback Interface


/***** Create callback interface **/private ActionMode. callback mActionMode = new ActionMode. callback () {@ Override public boolean onPrepareActionMode (ActionMode mode, Menu menu) {// TODO Auto-generated method stub return false;} @ Override public void onDestroyActionMode (ActionMode mode) {// TODO Auto-generated method stub if (mode! = Null) {mode = null ;}@ Override public boolean onCreateActionMode (ActionMode mode, Menu menu) {MenuInflater inflater = getMenuInflater (); // Add the Menu file inflater. inflate (R. menu. main_icon, menu); return true ;}@ Override public boolean onActionItemClicked (ActionMode mode, MenuItem item) {switch (item. getItemId () {case R. id. action_share: Log. d (tag, "action_share"); Toast. makeText (getApplicationContext (), "action_share", Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_save: Log. d (tag, "action_save"); Toast. makeText (getApplicationContext (), "action_save", Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_settings: Log. d (tag, "action_settings"); Toast. makeText (getApplicationContext (), "action_settings", Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_delete: Log. d (tag, "action_delete"); Toast. makeText (getApplicationContext (), "action_delete", Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_edit: Log. d (tag, "action_edit"); Toast. makeText (getApplicationContext (), "action_edit", Toast. LENGTH_SHORT ). show (); mode. finish (); break; default: break;} return true ;}};


3. cancel registration action mode

// Cancel registration of unRegisterForContextMenu (btnView );

XML file of Menu


<menu xmlns:android="http://schemas.android.com/apk/res/android" ><item    android:id="@+id/action_share"    android:orderInCategory="0"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_share"    android:title="share"/><item    android:id="@+id/action_save"    android:orderInCategory="1"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_save"    android:title="save"/><item    android:id="@+id/action_search"    android:orderInCategory="2"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_search"    android:title="search"/><item    android:id="@+id/action_delete"    android:orderInCategory="4"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_delete"    android:title="delectAction"/> <item    android:id="@+id/action_edit"    android:orderInCategory="5"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_edit"    android:title="edit"/>   <item    android:id="@+id/action_help"    android:orderInCategory="6"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_help"    android:title="help"/>      <item    android:id="@+id/action_compass"    android:orderInCategory="6"    android:showAsAction="ifRoom"    android:icon="@android:drawable/ic_menu_compass"    android:title="compass"/></menu>


In addition, the action mode can be combined with ListView and GridView.

Long-pressed ListView Item has the following effect:


The CSDN cannot upload the image.


Code Implementation


ListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE_MODAL); listView. setMultiChoiceModeListener (new MultiChoiceModeListener () {@ Override public boolean onPrepareActionMode (ActionMode mode, Menu menu) {// TODO Auto-generated method stub return false ;} @ Override public void onDestroyActionMode (ActionMode mode) {// TODO Auto-generated method stub} @ Override public boolean onCreateActionMode (ActionMode mode, Menu menu) {MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. main_icon, menu); return true ;}@ Override public boolean onActionItemClicked (ActionMode mode, MenuItem item) {switch (item. getItemId () {case R. id. action_share: Log. d (tag, "action_share"); // Modify the title mode. setTitle ("action_share"); Toast. makeText (getApplicationContext (), "action_share", Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_save: Log. d (tag, "action_save"); Toast. makeText (getApplicationContext (), "action_save", Toast. LENGTH_SHORT ). show (); mode. finish (); break; default: break;} return true;} @ Override public void onItemCheckedStateChanged (ActionMode mode, int position, long id, boolean checked) {// TODO Auto-generated method stub }});


Ii. Floating Mode

  1. Create menu
  2. Set Menu listening events

Specific Code Process

@ Overridepublic void onCreateContextMenu (android. view. contextMenu menu, View v, ContextMenuInfo menuInfo) {MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. main_icon, menu); super. onCreateContextMenu (menu, v, menuInfo) ;}@ Overridepublic boolean onContextItemSelected (MenuItem item) {// only partial response switch (item. getItemId () {case R. id. action_compass: break; case R. id. action_delete: break; default: break;} return super. onContextItemSelected (item );}

The XML file of Menu is the same as the action mode.

Although the Menu has icons, ContextMenu does not support icons, which are described on the official website.

In this way, the basic ContextMenu floating mode menu is completed.

Other methods related to ContextMenu



/***** Open the menu ***/@ Overridepublic void openContextMenu (View view) {// TODO Auto-generated method stub super. openContextMenu (view);}/*** close menu ***/@ Overridepublic void closeContextMenu () {// TODO Auto-generated method stub super. closeContextMenu ();}/*** listener event after the Menu is closed ***/@ Overridepublic void onContextMenuClosed (menu Menu) {super. onContextMenuClosed (menu );}



These methods can be selectively implemented based on your own needs.

ContextMenuWe will introduce you to related knowledge here. If you have any questions, please contact us.

Next IntroductionPopMenuRelated Knowledge






In android, when I press a subitem in listview and use the ContextMenu to pop up the menu, how can I get the data of this subitem?

Don't take it like this.
Listview. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
@ SuppressWarnings ("unchecked ")
HashMap <String, Object> map = (HashMap <String, Object>) arg0
. GetItemAtPosition (arg2 );
This map is the data of the item you clicked.

In Android development, how does registerForContextMenu differentiate the menus displayed by long-pressed items?

There is an ID.

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.