Android Development Series (19th): Use ContextMenu to create context menus

Source: Internet
Author: User

Android Development Series (19th): Use ContextMenu to create context menus

In the previous article, we introduced the options for creating a context menu. Here we will introduce how to create a context menu.

The context menu is to press a piece of text and then display the corresponding menu. For example, you can paste a text menu on a certain floor in Chang 'An. Click copy ", this text is saved in the temporary storage of your mobile phone. You can paste it elsewhere.

 

Steps of the development context menu:

1. Override the onCreateContextMenu (ContextMenu menu, View source, ContextMenu. Context MenuInfo menuInfo) method of the Activity.

2. Call the registerForContextMenu (View view) method of the Activity to register the context menu for the view component.

 

Next, we will explain it through a specific application.

First, create an Android project, and then edit the main. xml file:

 

In this XML file, we define a line of text. You can press this line to display the context menu and click the corresponding option to modify the background color.

 

 

Next, let's take a look at ContextMenuTest. java:

 

Import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. contextMenu; import android. view. menuItem; import android. view. view; import android. widget. textView; public class ContextMenuTest extends Activity {// define a final int MENU1 = 0x111 for each menu; final int MENU2 = 0x112; final int MENU3 = 0x113; private TextView txt; @ Overridepublic void onCreate (Bundle savedInstan CeState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); txt = (TextView) findViewById(R.id.txt); // register the context menu registerForContextMenu (txt) for the text box );} // triggered when the context menu is created @ Overridepublic void onCreateContextMenu (ContextMenu menu, View source, ContextMenu. contextMenuInfo menuInfo) {menu. add (0, MENU1, 0, "Red"); menu. add (0, MENU2, 0, "green"); menu. add (0, MENU3, 0, "blue"); // set the three menu items as single-choice menu items. setGroupChec Kable (0, true, true); // set the title and Icon menu of the context menu. setHeaderIcon (R. drawable. tools); menu. setHeaderTitle ("select background color");} // This method is triggered when the context menu is clicked. @ Overridepublic boolean onContextItemSelected (MenuItem mi) {switch (mi. getItemId () {case MENU1: mi. setChecked (true); txt. setBackgroundColor (Color. RED); break; case MENU2: mi. setChecked (true); txt. setBackgroundColor (Color. GREEN); break; case MENU3: mi. setChecked (true); txt. setBackgroundColor (Color. BLUE); break;} return true ;}}
First, override the onCreateContextMenu (ContextMenu menu, View source, ContextMenu. ContextMenuInfo menuInfo) method, and then add the corresponding menu options through the menu. add () method,

 

In addition, the onContextItemSelected (MenuItem mi) method is overwritten and called when the menu option is clicked.

 

Below is:

 

 

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.