1.菜单:
–系统菜单
—-onCreateOptionsMenu
–onOptionsItemSelected
—-为系统菜单添加图标
–上下文菜单:基于UI视图的
—-1.为UI视图注册上下文菜单:当在视图上长按时,会创建上下文菜单
—-2.重写onCreateContextMenu()
—-3.重写onContextItemSelected()
–弹出菜单(3.0开始出现)
—-自定义PopupMenu
—-给菜单添加菜单项
1)创建系统菜单
//menu接收的是系统创建的菜单对象
@Override
public boolean onCreateOptionsMenu(Menu menu) {
setIconEnable(menu,true);//让菜单项显示图标
// 创建系统菜单
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//选择系统菜单中某个菜单项的事件监听
//item接收的是被选择的菜单项对象
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO 添加事件监听
switch(item.getItemId())
{
case R.id.action_settings:
startActivity(new Intent(Settings.ACTION_SETTINGS));
break;
case R.id.action_add:
textView.setTextSize(textView.getTextSize()+5);
break;
case R.id.action_sub:
textView.setTextSize(textView.getTextSize()-5);
break;
}
return super.onOptionsItemSelected(item);
}
如果想要给每个选项添加一个图标,需要调用下面的方法
//为系统菜单项添加图标
//enable为true时,菜单添加图标有效,enable为false时无效。4.0+系统默认无效
private void setIconEnable(Menu menu, boolean enable)
{
try
{
Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
m.setAccessible(true);
//MenuBuilder实现Menu接口,创建菜单时,传进来的menu其实就是MenuBuilder对象(java的多态特征)
m.invoke(menu, enable);
} catch (Exception e)
{
e.printStackTrace();
}
}
2)创建上下文菜单
--为一个视图注册上下文菜单,长按该视图就会弹出菜单;
//1:为UI视图注册上下文菜单--当在textView上长按时会去创建上下文菜单
registerForContextMenu(textView);
//2:创建上下文菜单
//view接收的被注册的UI视图
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO 创建上下文菜单
getMenuInflater().inflate(R.menu.main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//3:选中上下文菜单中某个菜单项的事件
@Override
public boolean onContextItemSelected(MenuItem item) {
......
}
3)PopupMenu:弹出菜单,3.0开始出现的,所以api最低为11,
//第二个参数是弹出菜单显示的参考位置,通常在参考视图的下边显示
//创建一个弹出菜单对象
public void btnClick(View v)
{
PopupMenu pop = new PopupMenu(this, v);//第二个参数是菜单显示位置的参照物,这里是一个按钮
//给菜单添加菜单项
getMenuInflater().inflate(R.menu.main, pop.getMenu());
//给菜单项添加事件监听
pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_add:
fontSize+=5;
textView.setTextSize(fontSize);
break;
case R.id.action_sub:
fontSize-=5;
textView.setTextSize(fontSize);
break;
case R.id.action_background:
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
textView.setBackgroundColor(Color.rgb(red, green, blue));
break;
}
return false;
}
});
//显示菜单
pop.show();
}
4)ListView长按显示菜单:长按listView的一项,显示上下文菜单
实现1:
让listView实现OnCreateContextMenuListener接口,
1)
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, "新建");
menu.add(0, 1, 0, "删除");
menu.add(0, 2, 0, "编辑");
menu.setQwertyMode(true);
}
});
2)
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
int position = menuInfo.position;//menuInfo记录了长按选项的位置
Log.i("--", "position :" + position);
switch (item.getItemId()) {
case 0:
Toast.makeText(this, "新建", 0).show();
break;
case 1:
Toast.makeText(this, "删除", 0).show();
break;
case 2:
Toast.makeText(this, "编辑", 0).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
实现2:
让listView注册上下文菜单。
//注册上下文菜单
registerForContextMenu(listView);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo acm = (AdapterContextMenuInfo) menuInfo;
//得到当前长按的是listView中哪一个位置
currentItemPostion = acm.position;
getMenuInflater().inflate(R.menu.main, menu);
}
源码