android_菜单(Menu)的使用

这里介绍android.view.Menu的使用。

 

 

 

 

● 追加菜单的时候,覆盖(override)Activity.onCreateOptionsMenu()方法,并调用Menu.add()方法
● 追加菜单点击操作处理的时候,覆盖(override)Activity.onMenuItemSelected()方法

 

例程源码(Java)

01 public class MenuTestActivity extends Activity {
02     private static final int MENU_ID_TEST1 = (Menu.FIRST + 1);
03     private static final int MENU_ID_TEST2 = (Menu.FIRST + 2);
04      
05     /** Called when the activity is first created. */
06     @Override
07     public void onCreate(Bundle savedInstanceState) {
08         super.onCreate(savedInstanceState);
09         setContentView(R.layout.main);
10     }
11   
12     @Override
13     public boolean onCreateOptionsMenu(Menu menu) {
14         menu.add(Menu.NONE, MENU_ID_TEST1, Menu.NONE, "Menu1");
15         menu.add(Menu.NONE, MENU_ID_TEST2, Menu.NONE, "Menu2");
16          
17         return super.onCreateOptionsMenu(menu);
18     }
19   
20     @Override
21     public boolean onMenuItemSelected(int featureId, MenuItem item) {
22         boolean ret = true;
23   
24         switch (item.getItemId()) {
25         default:
26             ret = super.onMenuItemSelected(featureId, item);
27             break;
28         case MENU_ID_TEST1:
29             Log.v("Test", "Test1 was selected");
30             ret = true;
31             break;
32         case MENU_ID_TEST2:
33             Log.v("Test", "Test2 was selected");
34             ret = true;
35             break;
36         }
37   
38         return ret;
39     }  
40 }

参考资源
Menu | Android Developers
Activity | Android Developers

你可能感兴趣的:(android)