Android Learning Note--menu (ii)

Source: Internet
Author: User

    • Knowledge Points:

This time it will continue on an article without finishing the menu, context menu, and pop-up menus (popup menu).

    • Context Menu

The context menu provides actions on a particular item or context frame on the UI interface, just like the right-click menu in Windows.

In Android, there are two ways to provide contextual action: one is in a floating context menu (long press Eject) and the other is in context operation mode.

So how do you create a floating context menu (floating)?

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 in response to the event that the user long presses a menu item.

Here is a demo of the context menu:

1     PrivateTextView TextView;2     Final intRED = 0x101;3     Final intBLUE = 0x102;4     Final intGREEN = 0x103;5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9         Tentextview=(TextView) Findviewbyid (R.id.textview); One         //Registering a context menu for TextView A Registerforcontextmenu (textView); -     } - //Create a context menu the @Override -      Public voidOncreatecontextmenu (ContextMenu menu, View V, - contextmenuinfo Menuinfo) { -Menu.add (0, red, 0, "red"); +Menu.add (0, Blue, 0, "blue")); -Menu.add (0,green,0, "GREEN"); +Menu.setheadertitle ("Set The color of the TextView"); AMenu.setgroupcheckable (0,true,true); at     } -     //menu item Response event - @Override -      Public Booleanoncontextitemselected (MenuItem item) { -           Switch(Item.getitemid ()) { -          CaseGREEN: inItem.setchecked (true); - Textview.setbackgroundcolor (color.green); to              Break; +          CaseBLUE: -Item.setchecked (true); the Textview.setbackgroundcolor (color.blue); *              Break; $          CaseRED:Panax NotoginsengItem.setchecked (true); - Textview.setbackgroundcolor (color.red); the              Break; +         } A          return true; the}

    • Using the context operation mode

The context operation mode is to implement the System Actionmode interface, and focus on the user's interaction to implement the context operation behavior. When the user triggers this mode by selecting a menu item, a context action bar appears at the top of the screen for the user to manipulate the menu item you just selected. You can select multiple items (select multi items), deselect (deselect), action done, return (back), and more.

So when do I invoke the context mode of operation?

Called in the following two scenarios:

1. The user presses the view CEO (Long-click).

2. The user selects a check box (checkbox) or similar UI component in view.

How do I enable context mode of operation?

There are two different situations:

1. For stand-alone view

First implement ActionMode.Callback the interface, and then call the Startactionmode () method.

2. For ListView or Girdview (or other Abslistview expansion)

Implement AbsListView.MultiChoiceModeListener接口,调用setMultiChoiceModeListener()方法,最后传入CHOICE_MODE_MULTIPLE_MODAL参数调用setChoiceMode()方法。

1     PrivateListView ListView;2 @Override3     protected voidonCreate (Bundle savedinstancestate) {4         Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_main);6         7listview=(ListView) Findviewbyid (r.id.listview1);8String arr[]={"New York", "Shanghai", "Los Angle", "Beijing", "Paris", "Tykyo", "Moscow", "Berlin", "HongKong"};9         //wrapping an array into a arrayadapterTenArrayadapter<string> adapter1=NewArrayadapter<string> ( This, R.layout.array_item, arr); One Listview.setadapter (adapter1); A          - Listview.setchoicemode (listview.choice_mode_multiple_modal); -Listview.setmultichoicemodelistener (NewMultichoicemodelistener () { the              - @Override -              Public BooleanOnprepareactionmode (actionmode Mode, menu menu) { -                 //TODO auto-generated Method Stub +                 return false; -             } +              A @Override at              Public voidOndestroyactionmode (Actionmode mode) { -                 //TODO auto-generated Method Stub -                  -             } -              - @Override in              Public BooleanOncreateactionmode (actionmode Mode, menu menu) { -                 //TODO auto-generated Method Stub toMenuinflater Inflater =Mode.getmenuinflater (); + inflater.inflate (R.menu.contextmenu, menu); -                     return true; the             } *              $ @OverridePanax Notoginseng              Public Booleanonactionitemclicked (actionmode mode, MenuItem item) { -                Switch(Item.getitemid ()) { the              CaseR.id.done: +Toast.maketext (mainactivity. This, "you had clicked the Done", Toast.length_long). Show (); A                  Break; the              CaseR.id.cancel: +Toast.maketext (mainactivity. This, "You have clicked the Cancel", Toast.length_long). Show (); -             default: $                  Break; $             }     -                 return true; -             } the              - @OverrideWuyi              Public voidOnitemcheckedstatechanged (Actionmode mode,intposition, the                     LongIdBooleanchecked) { -                 //TODO auto-generated Method Stub Wu                  -             } About         }); $}
View Code

Android Learning Note--menu (ii)

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.