Android menu 2-context menu

Source: Internet
Author: User

The context menu is different from the option menu. The option menu serves the Activity, while the context menu is registered to a View object.

If a View object registers a context menu, you can press and hold the View object to call out the context menu.

The context menu does not support shortcut keys, and its menu options cannot contain icons. However, you can specify icons for the title of the context menu.

The following simulated context menu

Main. xml layout File

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<EditText android: text = "first text box"
Android: id = "@ + id/editText01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<EditText android: text = "second text box"
Android: id = "@ + id/editText02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>

MeunActivity class

Package com. ljq. activity;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. ContextMenu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. widget. EditText;

Public class MeunActivity extends Activity {
Private EditText editText01 = null;
Private EditText editText02 = null;

Private final int MENU1 = 1;
Private final int MENU2 = 2;
Private final int MENU3 = 3;
Private final int MENU4 = 4;
Private final int MENU5 = 5;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

EditText01 = (EditText) findViewById (R. id. editText01 );
EditText02 = (EditText) findViewById (R. id. editText02 );

// Register the context menu for the View object
This. registerForContextMenu (editText01 );
This. registerForContextMenu (editText02 );
}

/**
* Initialize the context menu
*
* The context menu is called once every time it is called.
*/
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
Menu. setHeaderIcon (R. drawable. header );
Switch (v. getId ()){
Case R. id. editText01:
Menu. add (0, MENU1, 0, "menu item 1 ");
Menu. add (0, MENU2, 0, "menu item 2 ");
Menu. add (0, MENU3, 0, "menu item 3 ");
Break;
Case R. id. editText02:
Menu. add (0, MENU4, 0, "menu item 4 ");
Menu. add (0, MENU5, 0, "menu item 5 ");
Break;
}
// Super. onCreateContextMenu (menu, v, menuInfo );
}

/**
* This event is called after the context menu option is selected.
*/
@ Override
Public boolean onContextItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case MENU1:
Case MENU2:
Case MENU3:
EditText01.append ("\ n" + item. getTitle () + "pressed ");
Break;
Case MENU4:
Case MENU5:
EditText02.append ("\ n" + item. getTitle () + "pressed ");
Break;
}
Return true;
}

}

Running result

1, 2,

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.