这里介绍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 ); |
05 |
/** Called when the activity is first created. */ |
07 |
public void onCreate(Bundle savedInstanceState) { |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.main); |
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" ); |
17 |
return super .onCreateOptionsMenu(menu); |
21 |
public boolean onMenuItemSelected( int featureId, MenuItem item) { |
24 |
switch (item.getItemId()) { |
26 |
ret = super .onMenuItemSelected(featureId, item); |
29 |
Log.v( "Test" , "Test1 was selected" ); |
33 |
Log.v( "Test" , "Test2 was selected" ); |
参考资源
● Menu | Android Developers
● Activity | Android Developers