Android除了可以通過菜單鍵彈出OptionsMenu外,還可以長按某個控件來彈出ContextMenu。比如EditeText就可以通過長按來彈出擁有“cut”,"copy","paste"等項的ContextMenu。
ContextMenu和OptionsMenu相比主要有以下區別:
1,ContextMenu必須通過Activity的
registerForContextMenu(View)來進行注冊,而OptionsMenu不用。
2,ContextMenu不支持icon,而OptionsMenu支持。
3,ContextMenu可以有頭,可以通過setHeaderIcon,setHeaderTitle,setHeaderView來設置頭,否則就沒有頭。
4,彈出的方式不一樣。
使用ContextMenu主要需要重載Activity的以下函數:
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
public void onContextMenuClosed (Menu menu)
public boolean onContextItemSelected (MenuItem item)
注意事項:ContextMenu的任何項在被選中後,整個ContextMenu會自動被系統關閉掉,即使多選項也是這樣。這樣的話,多選項的行為就有些怪異。查看Android的源碼後,發現這是個Android的bug,應該是google沒考慮多選項的情況。
實例1
文件ContextMenuActivity.java
package com.teleca;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ContextMenuActivity extends Activity {
View myContextMenuView=null;
View myContextMenuView2=null;
View myContextMenuView3=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myContextMenuView=this.findViewById(R.id.TextView1);
myContextMenuView2=findViewById(R.id.TextView2);
myContextMenuView3=findViewById(R.id.view3);
}
public void onResume()
{
super.onResume();
registerForContextMenu(myContextMenuView);
registerForContextMenu(myContextMenuView2);
registerForContextMenu(myContextMenuView3);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
System.out.println("onCreateContextMenu");
int groupId=0;
int order=Menu.NONE;
int itemId=0;
Resources res=getResources();
String title=null;
if(v.getId()==R.id.TextView1)
{
menu.add(groupId, R.id.menuItem1, order,R.string.menuItem1);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
else if(v.getId()==R.id.TextView2)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenub, menu);
}
else if (v.getId()==R.id.view3)
{
menu.add(groupId, R.id.menuItem1, order,R.string.menuItem1);
menu.add(groupId, R.id.menuItem2, order, R.string.menuItem2);
groupId++;
SubMenu subMenu=menu.addSubMenu(groupId, R.id.file, order, R.string.file);
subMenu.add(groupId, R.id.newFile, order, R.string.newFile);
subMenu.add(groupId, R.id.openFile, order, R.string.openFile);
groupId++;
menu.add(groupId, R.id.groupItem11, order, R.string.groupItem1);
menu.add(groupId, R.id.groupItem12, order, R.string.groupItem2);
menu.setGroupCheckable(groupId, false, false);
groupId++;
menu.add(groupId, R.id.groupItem21, order, R.string.groupItem1);
menu.add(groupId, R.id.groupItem22, order, R.string.groupItem2);
menu.setGroupCheckable(groupId, true, true);
groupId++;
menu.add(groupId, R.id.groupItem31, order, R.string.groupItem1);
menu.add(groupId, R.id.groupItem32, order, R.string.groupItem2);
menu.add(groupId, R.id.groupItem33, order, R.string.groupItem3);
menu.setGroupCheckable(groupId, true, false);
}
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle("My Context Menu");
}
public boolean onContextItemSelected (MenuItem item)
{
int id=item.getItemId();
if(id==R.id.menuItem1||id==R.id.menuItem2
||id==R.id.file||id==R.id.newFile||id==R.id.openFile
||id==R.id.group1)
{
System.out.println("menu:"+item.getTitle()+"is slected");
return true;
}
else if(item.isCheckable())
{
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
System.out.println("menu:"+item.getTitle()+"is cheked and slected");
return true;
}
else if(id==R.id.groupItem11||id==R.id.groupItem12)
{
System.out.println("menu Group:"+item.getTitle()+"is slected");
return true;
}
else
return super.onContextItemSelected(item);
}
final static String tag="robin";
public void onContextMenuClosed (Menu menu)
{
System.out.println("menu:is closed");
}
}
菜單文件1
menu\contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuItem2"
android:title="@string/menuItem2" />
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/newFile"
android:title="@string/newFile" />
<item android:id="@+id/openFile"
android:title="@string/openFile" />
</menu>
</item>
<!-- menu group -->
<group android:id="@+id/group1"
android:checkableBehavior="single">
<item android:id="@+id/groupItem11"
android:title="@string/groupItem1" />
<item android:id="@+id/groupItem12"
android:title="@string/groupItem2" />
</group>
</menu>
菜單文件2
menu\contextmenub.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuItem1"
android:title="@string/menuItem1" />
<item android:id="@+id/menuItem2"
android:title="@string/menuItem2" />
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/newFile"
android:title="@string/newFile" />
<item android:id="@+id/openFile"
android:title="@string/openFile" />
</menu>
</item>
<!-- menu group -->
<group android:id="@+id/group1"
android:checkableBehavior="none">
<item android:id="@+id/groupItem11"
android:title="@string/groupItem1" />
<item android:id="@+id/groupItem12"
android:title="@string/groupItem2" />
</group>
<group android:id="@+id/group2"
android:checkableBehavior="single">
<item android:id="@+id/groupItem21"
android:title="@string/groupItem1" />
<item android:id="@+id/groupItem22"
android:title="@string/groupItem2" />
</group>
<group android:id="@+id/group3"
android:checkableBehavior="all">
<item android:id="@+id/groupItem31"
android:title="@string/groupItem1" />
<item android:id="@+id/groupItem32"
android:title="@string/groupItem2" />
<item android:id="@+id/groupItem33"
android:title="@string/groupItem3" />
</group>
</menu>
字符串文件strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name_main">Main</string>
<string name="app_name">HelloWorld</string>
<string name="app_name2">HelloWorld2</string>
<string name="pressme">please press me!</string>
<string name="clone">clone me!</string>
<string name="ActivityInfo">print ActivityInfo!</string>
<string name="ContextMenuSample">static and dynamic ContextMenuSample</string>
<string name="staticContextMenuSample">staticContextMenuSample</string>
<string name="menuItem1">menuItem1</string>
<string name="menuItem2">menuItem2</string>
<string name="menuItem3">menuItem3</string>
<string name="file">file</string>
<string name="newFile">new file</string>
<string name="openFile">open</string>
<string name="groupItem1">groupItem1</string>
<string name="groupItem2">groupItem2</string>
<string name="groupItem3">groupItem3</string>
</resources>