创建androidMenu菜单
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 开始学习Android的开发第一节课学习的是如何实现简单的菜单,也好温习一下呵呵:)
2 引用头文件:
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.Menu;
6 import android.view.MenuItem;
7
8 public class hello extends Activity {
9
10 //定义菜单ID
11 public static final int ADD_ID = Menu.FIRST;
12 public static final int DELETE_ID = Menu.FIRST + 1;
13 public static final int EXIT_ID = Menu.FIRST + 2;
14
15 /**Crate Menu when the onCreate was called.*/
16 //当案系统Menu的时候系统开始调用此函数,主要用于创建MenuItem
17 @Override
18 public boolean onCreateOptionsMenu(Menu menu)
19 {
20 super.onCreateOptionsMenu(menu);
21 menu.add(0, ADD_ID, 0, R.string.menu_add);
22 menu.add(0, DELETE_ID, 1, R.string.menu_delete);
23 menu.add(0, EXIT_ID, 2, R.string.menu_exit);
24 return true;
25
26 }
27 //单击菜单项目的时候事件处理函数
28 public boolean onOptionsItemSelected(MenuItem item)
29 {
30
31 switch( item.getItemId() )
32 {
33 case ADD_ID:
34 this.setTitle("Insert
![](https://www.cnblogs.com/Images/dot.gif)
35 break;
36 case DELETE_ID:
37 this.setTitle("Delete
![](https://www.cnblogs.com/Images/dot.gif)
38 break;
39 case EXIT_ID:
40 this.finish();
41 break;
42 default:
43 break;
44 }
45 return super.onOptionsItemSelected( item );
46
47 }
48
49 /** Called when the activity is first created. */
50 //这是系统调用的应用程序入口
51 @Override
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.main);
55 }
56 }