Difference between the use of android -- ContextMenu and OptionsMenu

Source: Internet
Author: User

Menus are one of the most common elements in the user interface and are frequently used. In Android, menus are divided into three types: OptionsMenu and ContextMenu) and SubMenu (SubMenu). Today we will talk about ContextMenu.

 

1. Select OptionsMenu
Click Menu key to display the selection Menu
Implementation Method. onCreateOptionsMenu () This method will be called only once, that is, it will be called at the first display.
If you want to update the menu items, you can operate in the onPrepareOptionsMenu () method.
When the menu is selected, it is implemented in the OnOptionsItemSelected () method.

2. Context Menu ContextMenu
When the view is pressed for 2 s, the context menu. onCreateContextMenu is displayed and called each time. The context menu is selected and implemented in the onContextItemSelected () method.

 

Overview:

The context menu of Android is similar to the context menu on PC. After a context menu is registered for a view, a floating menu is displayed, that is, the context menu, after a long press (about 2 seconds. Context menus can be registered for any view. However, the most common option is the item used for list view ListView.

Note: The context menu of Android does not support icons or shortcut keys.

 

To create a context menu:

1. overwrite the onCreateContenxtMenu () method of the Activity, and call the add method of Menu to add a Menu item (MenuItem ).

2. overwrite the onContextItemSelected () method of the Activity and respond to the Click Event of the context menu item.

3. Call the registerForContextMenu () method to register the context menu for the view.

 

Example:

MainActivity. java file:

//file name: MainActivity.java
package hi.braincol.local.contextMenu;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.util.Log;
public class MainActivity extends ListActivity {
    
    private static final int ITEM1 = Menu.FIRST;
    private static final int ITEM2 = Menu.FIRST+1;
    private static final int ITEM3 = Menu.FIRST+2;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        showListView();
        
// Register ContextMenu for all items in ListView
        registerForContextMenu(getListView());
// The registerForContextMenu () here can also be replaced by the following statement
        //getListView().setOnCreateContextMenuListener(this);
    }
    
    private void showListView(){
        String[] mString = new String[]{
Lufei-Monkey D Luffy,
Nami,
Zhuo lo-Zoro,
Shan zhi-Sanji,
Niko Robin-Ms. All Sunday,
Usop-usoppu,
Tony Joba-Tony Chopper,
        };
        ArrayAdapter
 
   mla = new ArrayAdapter
  
   (MainActivity.this, 
  
 
                R.layout.main, mString);
        MainActivity.this.setListAdapter(mla);      
    }
    
// Context menu. The context menu will be activated through long-pressed entries.
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, 
            ContextMenuInfo menuInfo) {
Menu. setHeaderTitle (profile );
// Add a menu item
Menu. add (0, ITEM1, 0, special );
Menu. add (0, ITEM2, 0, fighting power );
Menu. add (0, ITEM3, 0, classic quotes );
    }
    
// Click response in the menu
    @Override
    public boolean onContextItemSelected(MenuItem item){
// Obtain the information of the selected menu item
        //AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        //Log.i(braincol,String.valueOf(info.id));        
        switch(item.getItemId()){
        case ITEM1:
// Add the processing code here
            
            break;
        case ITEM2:
// Add the processing code here
            
            break;    
        case ITEM3:
// Add the processing code here
            
            break;
        }
        return true;
    }
    
}

Main. xml layout file:

 
 
  
    android:id=@+id/myTextView
    android:textSize=20sp
    android:layout_width=fill_parent
    android:layout_height=fill_parent
    android:text=@string/hello
    />

Running result:

 

OnCreateOptionsMenu --> image text

 

1 Method 1: The text icon is planned in xml, and the text is defined in string. xml: The image is called directly from the project file. For all text and image formatting, it is placed in a separate menu. xml file

[Java]
  1. @ Override public boolean onCreateOptionsMenu (Menu menu ){
  2. Super. onCreateOptionsMenu (menu); MenuInflater menuInflater = getMenuInflater ();
  3. MenuInflater. inflate (R. menu. menu, menu); return true;
  4. }[Html]
      • Vc2VTdG9w ">
      •  


      2. dynamically create menus

      [Java]
      1. @ Override public boolean onCreateOptionsMenu (Menu menu ){
      2. // TODO Auto-generated method stub menu. add (0, LOCATION, 1, R. string. location );
      3. Menu. add (0, SEARCH, 2, R. string. search); menu. add (0, SHOWROUTE, 3, R. string. vechicleLocation );
      4. Menu. add (0, ALLROUTE, 4, R. string. allRoute); return super. onCreateOptionsMenu (menu );
      5. }[Java]
        1. Public boolean onCreateOptionsMenu (Menu menu ){
        2. Menu. add (0, CLASSIC_MENU_REFRESH, 0, R. string. menu_refresh). setIcon (R. drawable. cmcc_toolbar_refresh); return super. onCreateOptionsMenu (menu );
        3. }
           

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.