Android Learning Notes--menu Introduction (ii) _android

Source: Internet
Author: User
Tags stub

Knowledge Points:

This will continue with the previous article on the Learning of menu, context menu and pop-up menu.

Context Menu

The context menu provides action on a specific 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 to respond to events in which the user is long by menu item.

Here is a demo of a context menu:

Private TextView TextView;
  Final int RED = 0x101;
  Final int BLUE = 0x102;
  Final int GREEN = 0x103;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    
    Setcontentview (R.layout.activity_main);
    textview= (TextView) Findviewbyid (R.id.textview);
  Register context menu for TextView registerforcontextmenu (TextView);
    //Create context menu @Override public void Oncreatecontextmenu (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");
  Menu.setgroupcheckable (0, True, true); }//menu item Response Event @Override public boolean oncontextitemselected (MenuItem item) {switch (Item.getitemid ()) {CAs
      E GREEN:item.setChecked (TRUE);
      Textview.setbackgroundcolor (Color.green);
    Break
      Case BLUE:item.setChecked (TRUE); Textview.setbackgroundcolor (ColoR.blue);
    Break
      Case RED:item.setChecked (TRUE);
      Textview.setbackgroundcolor (color.red);
    Break
  return true; }

Use context action mode

The context operation mode is to implement the Actionmode interface of the system, and focus on the user's interaction to implement the context operation behavior. When a user triggers this pattern by selecting a menu item, a context action bar that allows the user to manipulate the menu item just selected appears at the top of the screen. Users can select multiple items (select multi items), deselect (deselect), perform operations (done), return (back), and so on.

So when do I call context-mode?

Called in the following two cases:

1. The user to the view CEO Press (Long-click) operation.

2. The user selects a checkbox or similar UI component in view.

How do I enable the context mode of operation?

This can be divided into two different situations:

1. For independent view

The Actionmode.callback interface is implemented first, and then the Startactionmode () method is invoked.

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

Implement the Abslistview.multichoicemodelistener interface, call the Setmultichoicemodelistener () method, and finally pass in the Choice_mode_multiple_ The modal parameter calls the Setchoicemode () method.

Private ListView ListView;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    
    Setcontentview (R.layout.activity_main);
    listview= (ListView) Findviewbyid (R.ID.LISTVIEW1);
    String arr[]={"New York", "Shanghai", "Los Angle", "BeiJing", "Paris", "Tykyo", "Moscow", "Berlin", "Hongkong"}; Wrap the array into Arrayadapter arrayadapter<string> adapter1= new arrayadapter<string> (This,r.layout.array_item,
    ARR);
    
    Listview.setadapter (Adapter1);
    Listview.setchoicemode (Listview.choice_mode_multiple_modal); Listview.setmultichoicemodelistener (New Multichoicemodelistener () {@Override public boolean Onpreparea
      Ctionmode (actionmode Mode, menu menu) {//TODO auto-generated method stub return false; @Override public void Ondestroyactionmode (Actionmode mode) {//TODO auto-generated Method St UB} @Override Public BooleAn Oncreateactionmode (actionmode mode, menu menu) {//TODO auto-generated method stub Menuinflater INFL
          Ater = Mode.getmenuinflater ();
          Inflater.inflate (R.menu.contextmenu, menu);
      return true; @Override public boolean onactionitemclicked (actionmode mode, MenuItem Item) {Switch (ITEM.G Etitemid ()) {case R.id.done:toast.maketext (mainactivity.this, "your have clicked the Done", Toast.length_lo
        NG). Show ();
      Break
      Case R.id.cancel:toast.maketext (Mainactivity.this, "Your have clicked the Cancel", Toast.length_long). Show ();
      Default:break;
      return true; @Override public void onitemcheckedstatechanged (actionmode mode, int position, long id, Boo
  Lean checked) {//TODO auto-generated Method stub}}); }

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.