standupTimer项目中菜单的应用很常规,没有什么难的地方,新手一看就懂,这里简单把代码贴到下面来;
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/settings"
android:alphabeticShortcut="s"
android:icon="@android:drawable/ic_menu_preferences"
android:title="@string/settings_label"/>
<item
android:id="@+id/teams"
android:alphabeticShortcut="t"
android:icon="@drawable/team"
android:title="@string/team_label"/>
<item
android:id="@+id/about"
android:alphabeticShortcut="a"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/about_label"/>
<item
android:id="@+id/help"
android:alphabeticShortcut="h"
android:icon="@android:drawable/ic_menu_help"
android:title="@string/help_label"/>
</menu></span>
再在onCreateOptionsMenu()方法中把xml的菜单文件填充进来:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
然后在实现菜单中各个条目对应的功能,功能实现在onOptionsItemSelected()方法中:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Logger.d("Displaying the about box");
displayAboutBox();
return true;
case R.id.help:
Logger.d("Displaying the help dialog");
displayHelpDialog();
return true;
case R.id.settings:
Logger.d("Displaying the settings");
displaySettings();
return true;
case R.id.teams:
Logger.d("Displaying the team configuration");
displayTeamConfiguration();
return true;
default:
Logger.e("Unknown menu item selected");
return false;
}
}
这些只是选项菜单的使用,而在Android中除了选项菜单(OptionsMenu)之外,还有上下文菜单(也称为长按菜单)( ContextMenu)和子菜单(SubMenu);关于上下文菜单和子菜单的使用,我会在另外的博客中做下大致的总结!