Context menu (ContextMenu) use case in Android

Source: Internet
Author: User

In Android, hold down a control (like a text box TextView, a button is a control) popup menu for context menu, create a context menu divided into the following steps:
1, overwrite the Oncreatecontextmenu method, generate the corresponding menu sub-options.
2. Overwrite the Oncontextitemselected method to generate response events for the corresponding sub-options.
3. Register the context menu for a control.
Let's look at a small example:
1. Add a TextView control in the mainactivity XML file with the ID MYTEXTV:

<TextView android:id="@+id/myTextV"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />

2, write the corresponding code in Mainactivity:

 PackageCom.yangzi.contextmenu;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.ContextMenu;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ContextMenu.ContextMenuInfo;ImportAndroid.widget.TextView; Public  class mainactivity extends Activity {    //Declaration menu sub-option ID number    Private Static Final intITEM1 = Menu.first;Private Static Final intITEM2 = ITEM1 +1;Private Static Final intITEM3 = item1+2;//Declaration TextView Control    PrivateTextView MYTEXTV;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Find text Display controlMYTEXTV = (TextView) Findviewbyid (R.ID.MYTEXTV);//Register the context menu for the text controlRegisterforcontextmenu (MYTEXTV); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }/ * * Overwrite the Oncreatecontextmenu method and set up an afternoon menu * * /    @Override     Public void Oncreatecontextmenu(ContextMenu menu, View V, Contextmenuinfo menuinfo) {/* * The parameter description of the Add method: * Parameter one: Group ID (int): Set ID, which indicates which group the sub-option belongs to * parameter two: item ID (int): Sub-option ID, which is used to indicate the sub-option          * Parameter three: order ID (int): Display order, indicating the display order of the sub-option, default to 0, indicating in addition order * parameter four: title (String): Used to define the display text of the sub-option * * */Menu.add (0, ITEM1,0,"Red Background"); Menu.add (0, ITEM2,0,"Yellow Background"); Menu.add (0, ITEM3,0,"Background Blue"); }/ * Overwrite the method and add the corresponding menu event * */    @Override     Public Boolean oncontextitemselected(MenuItem Item) {/* * Different responses based on selected sub-options (by item ID) * * The color resources used here are declared in the Color.xml file within the Res/values folder * * */        Switch(Item.getitemid ()) { CaseITEM1://Set the background of the text display controlMytextv.setbackgroundcolor (Getresources (). GetColor (r.color.red)); Break; CaseITEM2:myTextV.setBackgroundColor (Getresources (). GetColor (R.color.yellow)); Break; CaseITEM3:myTextV.setBackgroundColor (Getresources (). GetColor (R.color.white)); Break;default: Break; }return true; }}

Try it yourself and see how it works!

Context menu (ContextMenu) use case in Android

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.