Android learning notes-Menu (2), android learning notes

Source: Internet
Author: User

Android learning notes-Menu (2), android learning notes

  • Knowledge point:

This time, we will continue the learning of menus not finished in the previous article, Context Menu and pop-up menu ).

  • Context Menu

The Context Menu provides operations on specific items or context frameworks on the UI interface, just like the context menu in Windows.

In Android, there are two ways to provide context operations: one is in the floating context menu (long-pressed pop-up), and the other is in the Context operation mode.

So how to create a floating context menu?

1. Call the registerForContextMenu () method to register the context menu for the View.

2. Implement the onCreateContextMenu () method in Activity or Fragment.

3. Implement the onContextItemSelected () method to respond to the user's long-pressed menu item event.

The following is a context menu demo:

1 private TextView textView; 2 final int RED = 0x101; 3 final int BLUE = 0x102; 4 final int GREEN = 0x103; 5 @ Override 6 protected void onCreate (Bundle savedInstanceState) {7 super. onCreate (savedInstanceState); 8 setContentView (R. layout. activity_main); 9 10 textView = (TextView) findViewById (R. id. textView); 11 // register the context menu 12 registerForContextMenu (textView) for textView; 13} 14 // create the context menu 15 @ Override16 public void onCreateContextMenu (ContextMenu menu, View v, 17 ContextMenuInfo menuInfo) {18 menu. add (0, RED, 0, "Red"); 19 menu. add (0, BLUE, 0, "Blue"); 20 menu. add (0, GREEN, 0, "Green"); 21 menu. setHeaderTitle ("Set the color of the textView"); 22 menu. setGroupCheckable (0, true, true); 23} 24 // menu item RESPONSE event 25 @ Override26 public boolean onContextItemSelected (MenuItem item) {27 switch (item. getItemId () {28 case GREEN: 29 item. setChecked (true); 30 textView. setBackgroundColor (Color. GREEN); 31 break; 32 case BLUE: 33 item. setChecked (true); 34 textView. setBackgroundColor (Color. BLUE); 35 break; 36 case RED: 37 item. setChecked (true); 38 textView. setBackgroundColor (Color. RED); 39 break; 40} 41 return true; 42}

  • Use context Mode

The context operation mode is used to implement the system ActionMode interface and focus on user interaction to implement context operations. When you select a menu item to trigger this mode, a context action bar that allows you to operate on the selected menu item appears at the top of the screen. You can select multiple items (select multi items), deselect, Done, and BACK.

When will the context operation mode be called?

In the following two cases:

1. the user performs long-click operations on The View.

2. You have selected check boxes or similar UI components in the View.

How to enable the context operation mode?

There are two cases:

1. Independent View

First, implementActionMode.CallbackAnd then call the startActionMode () method.

2. For ListView or GirdView (or other AbsListView extensions)

ImplementationAbsListView. MultiChoiceModeListener interface, call the setMultiChoiceModeListener () method, and pass in the CHOICE_MODE_MULTIPLE_MODAL parameter to call the setChoiceMode () method.

1 private ListView; 2 @ Override 3 protected void onCreate (Bundle savedInstanceState) {4 super. onCreate (savedInstanceState); 5 setContentView (R. layout. activity_main); 6 7 listView = (ListView) findViewById (R. id. listview1); 8 String arr [] = {"New York", "Shanghai", "Los Angle", "BeiJing", "Paris", "Tykyo", "Moscow ", "Berlin", "HongKong"}; 9 // wrap the array into ArrayAdapter10 ArrayAdapter <String> adapter1 = new ArrayAdapter <String> (this, R. layout. array_item, arr); 11 listView. setAdapter (adapter1); 12 13 listView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE_MODAL); 14 listView. setMultiChoiceModeListener (new MultiChoiceModeListener () {15 16 @ Override17 public boolean onPrepareActionMode (ActionMode mode, Menu menu) {18 // TODO Auto-generated method stub19 return false; 20} 21 22 @ Override23 public void onDestroyActionMode (ActionMode mode) {24 // TODO Auto-generated method stub25 26} 27 28 @ Override29 public boolean onCreateActionMode (ActionMode mode, Menu menu) {30 // TODO Auto-generated method stub31 MenuInflater inflater = mode. getMenuInflater (); 32 inflater. inflate (R. menu. contextmenu, menu); 33 return true; 34} 35 36 @ Override37 public boolean onActionItemClicked (ActionMode mode, MenuItem item) {38 switch (item. getItemId () {39 case R. id. done: 40 Toast. makeText (MainActivity. this, "You have clicked the DONE", Toast. LENGTH_LONG ). show (); 41 break; 42 case R. id. cancel: 43 Toast. makeText (MainActivity. this, "You have clicked the CANCEL", Toast. LENGTH_LONG ). show (); 44 default: 45 break; 46} 47 return true; 48} 49 50 @ Override51 public void onItemCheckedStateChanged (ActionMode mode, int position, 52 long id, boolean checked) {53 // TODO Auto-generated method stub54 55} 56}); 57}View Code


Writing a Button in the android program is equivalent to pressing the menu key.

The android control is very convenient. You can directly add a button to listen to it to implement setOnClickListener (). Just put what you do on the menu in this listener.

What are the meanings of the four parameters in menuadd (1, 0, 0, Rstringreply) in Android development?

The first int type group ID parameter represents the group concept. You can group several menu items to better manage your menu buttons in a group.
The item ID parameter of the second int type represents the project number. This parameter is very important. An item ID corresponds to an option in a menu. This item ID is used to determine which option you are using when using the menu later.
The third 'order id' parameter of 'int' type represents the order in which menu items are displayed. The default value is 0, indicating that the display order of the menu is displayed according to the display order of add.
The fourth String type title parameter, indicating the text displayed in the option.

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.