本文转自:http://www.oschina.net/code/snippet_163910_6068
package com.baosight.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
/**
* 选项菜单软件开发
* @author shyboy(897948924@qq.com)
*/
public class OptionMenuActivity extends Activity {
/**
* 菜单项ID常量
*/
private static final int ITEM1 = Menu.FIRST;
private static final int ITEM2 = Menu.FIRST + 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**
* 覆盖该方法添加菜单项
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, ITEM1, 0, "开始");
menu.add(0, ITEM2, 0, "退出");
return true;
}
/**
* 覆盖该方法,响应菜单选项被单击事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/**
* 当“开始”菜单被选择时
*/
case ITEM1:
this.setTitle("开始游戏");
break;
/**
* 当“退出”菜单被选择时
*/
case ITEM2:
this.setTitle("退出游戏");
break;
}
return true;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
/**
* 选项菜单软件开发
* @author shyboy(897948924@qq.com)
*/
public class OptionMenuActivity extends Activity {
/**
* 菜单项ID常量
*/
private static final int ITEM1 = Menu.FIRST;
private static final int ITEM2 = Menu.FIRST + 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**
* 覆盖该方法添加菜单项
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, ITEM1, 0, "开始");
menu.add(0, ITEM2, 0, "退出");
return true;
}
/**
* 覆盖该方法,响应菜单选项被单击事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/**
* 当“开始”菜单被选择时
*/
case ITEM1:
this.setTitle("开始游戏");
break;
/**
* 当“退出”菜单被选择时
*/
case ITEM2:
this.setTitle("退出游戏");
break;
}
return true;
}
}