Activity类中的onCreateOptionsMenu事件方法用来创建选项菜单,该方法的定义如下:
public boolean onCreateOptionsMenu(Menu menu);
一般需要将创建选项菜单的代码放在onCreateOptionsMenu方法中,通过menu.add方法可以添加一个选项菜单项。该方法有4中重载形式:
public abstract MenuItem add(CharSequence title)
public abstract MenuItem add(int titleRes)
public abstract MenuItem add(int groupId, int itemId, int order,CharSequence title)
public abstract MenuItem add(int groupId, int itemId, int order, int titleRes)
groupId:菜单项的分组ID,该参数一般用于带选项按钮的菜单。参数值可以是负整数、0和正整数。
itemId:当前添加的菜单项的ID。该参数值可以是负整数、0和正整数。
order:菜单显示顺序,参数值必须是0和正整数,不能为负整数,采用从左到右,从上到下显示。
title或者titleRes:菜单项标题的stringID或者字符串。
1.添加选项菜单项
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem addMenuItem = menu.add(1, 1, 1, "添加");
MenuItem okMenuItem = menu.add(1, 2, 2, "OK");
MenuItem deleteMenuItem = menu.add(1, 3, 3, "删除");
return true;
}
2.给选项菜单项添加图标
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem addMenuItem = menu.add(1, 1, 1, "添加");
MenuItem okMenuItem = menu.add(1, 2, 2, "OK");
MenuItem deleteMenuItem = menu.add(1, 3, 3, "删除");
return true;
}
3.关联Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem addMenuItem = menu.add(1, 1, 1, "添加");
MenuItem okMenuItem = menu.add(1, 2, 2, "OK");
MenuItem deleteMenuItem = menu.add(1, 3, 3, "删除");
addMenuItem.setIcon(R.drawable.add);
okMenuItem.setIcon(R.drawable.ok);
deleteMenuItem.setIcon(R.drawable.delete);
//将DeleteActivity与delete菜单项进行关联
deleteMenuItem.setIntent(new Intent(this, DeleteActivity.class));
return true;
}
4.响应菜单的单击动作
package cn.yh.menufirst;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnMenuItemClickListener{
private MenuItem addMenuItem;
private MenuItem okMenuItem;
private MenuItem deleteMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
addMenuItem = menu.add(1, 1, 1, "添加");
okMenuItem = menu.add(1, 2, 2, "OK");
deleteMenuItem = menu.add(1, 3, 3, "删除");
addMenuItem.setIcon(R.drawable.add);
okMenuItem.setIcon(R.drawable.ok);
deleteMenuItem.setIcon(R.drawable.delete);
//给addMenuItem、deleteMenuItem和okMenuItem设置单击事件监听
addMenuItem.setOnMenuItemClickListener(this);
okMenuItem.setOnMenuItemClickListener(this);
deleteMenuItem.setOnMenuItemClickListener(this);
return true;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
startActivity(new Intent(this, AddActivity.class));
break;
case 2:
startActivity(new Intent(this, OkActivity.class));
break;
case 3:
startActivity(new Intent(this, DeleteActivity.class));
break;
default:
break;
}
return true;
}
}
当在onMenuItemClick返回true时,则与菜单项关联的Activity失效。也就是说,系统调用单击事件方法后,就不会调用startActivity方法显示与菜单项相关联的Activity。如下:
package cn.yh.menufirst;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnMenuItemClickListener{
private MenuItem addMenuItem;
private MenuItem okMenuItem;
private MenuItem deleteMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
addMenuItem = menu.add(1, 1, 1, "添加");
okMenuItem = menu.add(1, 2, 2, "OK");
deleteMenuItem = menu.add(1, 3, 3, "删除");
addMenuItem.setIcon(R.drawable.add);
okMenuItem.setIcon(R.drawable.ok);
deleteMenuItem.setIcon(R.drawable.delete);
//将DeleteActivity与delete菜单项进行关联
deleteMenuItem.setIntent(new Intent(this, DeleteActivity.class));
//给addMenuItem、deleteMenuItem和okMenuItem设置单击事件监听
addMenuItem.setOnMenuItemClickListener(this);
okMenuItem.setOnMenuItemClickListener(this);
deleteMenuItem.setOnMenuItemClickListener(this);
return true;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
startActivity(new Intent(this, AddActivity.class));
break;
case 2:
startActivity(new Intent(this, OkActivity.class));
break;
case 3:
// startActivity(new Intent(this, DeleteActivity.class));
Toast.makeText(this, R.string.onMenuItemClickDelete, Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
}
这时候:当单击delete菜单项时,只会提示R.string.onMenuItemClickDelete内容,而不会打开DeleteActivity。
5.使用Activity的onMenuItemSelected和onOptionsItemSelected方法来响应菜单项的单击事件
public boolean onMenuItemSelected(int featureId, MenuItem item);
public boolean onOptionsItemSelected(MenuItem item);
这两个方法都有一个item参数,用来传递被单击的菜单项的MenuItem参数对象。