ContextMenu使用实例

Android除了可以通过菜单键弹出OptionsMenu外,还可以长按某个控件来弹出ContextMenu。比如EditeText就可以通过长按来弹出拥有“cut”,"copy","paste"等项的<wbr style="line-height:25px">ContextMenu。</wbr>
ContextMenu和OptionsMenu相比主要有以下区别:
1,ContextMenu必须通过Activity的 registerForContextMenu(View)来进行注册,而OptionsMenu不用。
2,ContextMenu不支持icon,而OptionsMenu支持。
3,ContextMenu可以有头,可以通过setHeaderIcon,setHeaderTitle,setHeaderView来设置头,否则就没有头。
4,弹出的方式不一样。
使用ContextMenu主要需要重载Activity的以下函数:
public voidonCreateContextMenu(ContextMenumenu,Viewv,ContextMenu.ContextMenuInfomenuInfo)
public voidonContextMenuClosed(Menumenu)
public booleanonContextItemSelected(MenuItemitem)
注意事项: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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值