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]
- @ Override public boolean onCreateOptionsMenu (Menu menu ){
- Super. onCreateOptionsMenu (menu); MenuInflater menuInflater = getMenuInflater ();
- MenuInflater. inflate (R. menu. menu, menu); return true;
- }[Html]
-
2. dynamically create menus
[Java]
- @ Override public boolean onCreateOptionsMenu (Menu menu ){
- // TODO Auto-generated method stub menu. add (0, LOCATION, 1, R. string. location );
- Menu. add (0, SEARCH, 2, R. string. search); menu. add (0, SHOWROUTE, 3, R. string. vechicleLocation );
- Menu. add (0, ALLROUTE, 4, R. string. allRoute); return super. onCreateOptionsMenu (menu );
- }[Java]
- Public boolean onCreateOptionsMenu (Menu menu ){
- Menu. add (0, CLASSIC_MENU_REFRESH, 0, R. string. menu_refresh). setIcon (R. drawable. cmcc_toolbar_refresh); return super. onCreateOptionsMenu (menu );
- }