android开发步步为营之29:选项菜单OptionsMenu的用法
2014-09-23 22:02
429 查看
菜单也是一个非常常用的主键,点击手机上的菜单按钮就呼出了。在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),今天这讲是OptionsMenu
public interface Menu
android.view.Menu
Known Indirect Subclasses
ContextMenu, SubMenu
ContextMenu Extension of Menu for context menus providing functionality to modify the header of the context menu.
SubMenu Subclass of Menu for sub menus.
Class Overview
Interface for managing the items in a menu.
By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching
code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem).
Different menu types support different features:
Context menus: Do not support item shortcuts and item icons.
Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item
check marks are discouraged.
Sub menus: Do not support item icons, or nested sub menus.
开始我们的练习:创建一个OptionsMenu菜单
第一步、设计界面menuview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content" android:text="点击menu键选择菜单" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"></TextView>
</RelativeLayout>
第二步、设计Activity OptionsMenuActivity.java
/**
*
*/
package com.figo.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
* @author zhuzhifei
*
*/
public class OptionsMenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menuview);
}
/* *
* add()方法的四个参数,依次是: * 1、组别,如果不分组的话就写Menu.NONE, * 2、Id,这个很重要,
* Android根据这个Id来确定不同的菜单 * 3、顺序,那个菜单现在在前面由这个参数的大小决定 4、文本,菜单的显示文本
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Toast.makeText(this, "onCreateOptionsMenu", Toast.LENGTH_LONG).show();
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(
android.R.drawable.ic_menu_delete);
// setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
// android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "编辑").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(
android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, Menu.FIRST + 7, 7, "电话").setIcon(//多余6项将显示more,点击more后将显示更多的菜单
android.R.drawable.ic_menu_call);
menu.add(Menu.NONE, Menu.FIRST + 8, 8, "照相").setIcon(
android.R.drawable.ic_menu_camera);
return true;
}
//点击Menu按钮时,先执行准备工作
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// return super.onPrepareOptionsMenu(menu);
Toast.makeText(this, "onPrepareOptionsMenu", Toast.LENGTH_LONG).show();
return true;
}
//再执行打开动作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onOptionsItemSelected(item);
//点击后一般执行startActivity(intent);启动某个Activity,这里只演示
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "编辑菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 7:
Toast.makeText(this, "电话菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 8:
Toast.makeText(this, "照相菜单被点击了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
@Override
public void openOptionsMenu() {
// TODO Auto-generated method stub
super.openOptionsMenu();
Toast.makeText(this, "openOptionsMenu", Toast.LENGTH_LONG).show();
}
//然后执行关闭菜单选项工作
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
Toast.makeText(this, "onOptionsMenuClosed", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
// TODO Auto-generated method stub
Toast.makeText(this, "onCreatePanelMenu", Toast.LENGTH_LONG).show();
return super.onCreatePanelMenu(featureId, menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(this, "onMenuItemSelected", Toast.LENGTH_LONG).show();
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
Toast.makeText(this, "onMenuOpened", Toast.LENGTH_LONG).show();
return super.onMenuOpened(featureId, menu);
}
}
第三步、AndroidManifest.xml注册Activity
<activity android:name=".OptionsMenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步、运行效果
![](http://img.blog.csdn.net/20140923220335742?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmlnbzA0MjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
public interface Menu
android.view.Menu
Known Indirect Subclasses
ContextMenu, SubMenu
ContextMenu Extension of Menu for context menus providing functionality to modify the header of the context menu.
SubMenu Subclass of Menu for sub menus.
Class Overview
Interface for managing the items in a menu.
By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching
code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem).
Different menu types support different features:
Context menus: Do not support item shortcuts and item icons.
Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item
check marks are discouraged.
Sub menus: Do not support item icons, or nested sub menus.
开始我们的练习:创建一个OptionsMenu菜单
第一步、设计界面menuview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content" android:text="点击menu键选择菜单" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"></TextView>
</RelativeLayout>
第二步、设计Activity OptionsMenuActivity.java
/**
*
*/
package com.figo.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
* @author zhuzhifei
*
*/
public class OptionsMenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menuview);
}
/* *
* add()方法的四个参数,依次是: * 1、组别,如果不分组的话就写Menu.NONE, * 2、Id,这个很重要,
* Android根据这个Id来确定不同的菜单 * 3、顺序,那个菜单现在在前面由这个参数的大小决定 4、文本,菜单的显示文本
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Toast.makeText(this, "onCreateOptionsMenu", Toast.LENGTH_LONG).show();
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(
android.R.drawable.ic_menu_delete);
// setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
// android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "编辑").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(
android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, Menu.FIRST + 7, 7, "电话").setIcon(//多余6项将显示more,点击more后将显示更多的菜单
android.R.drawable.ic_menu_call);
menu.add(Menu.NONE, Menu.FIRST + 8, 8, "照相").setIcon(
android.R.drawable.ic_menu_camera);
return true;
}
//点击Menu按钮时,先执行准备工作
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// return super.onPrepareOptionsMenu(menu);
Toast.makeText(this, "onPrepareOptionsMenu", Toast.LENGTH_LONG).show();
return true;
}
//再执行打开动作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onOptionsItemSelected(item);
//点击后一般执行startActivity(intent);启动某个Activity,这里只演示
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "编辑菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 7:
Toast.makeText(this, "电话菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 8:
Toast.makeText(this, "照相菜单被点击了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
@Override
public void openOptionsMenu() {
// TODO Auto-generated method stub
super.openOptionsMenu();
Toast.makeText(this, "openOptionsMenu", Toast.LENGTH_LONG).show();
}
//然后执行关闭菜单选项工作
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
Toast.makeText(this, "onOptionsMenuClosed", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
// TODO Auto-generated method stub
Toast.makeText(this, "onCreatePanelMenu", Toast.LENGTH_LONG).show();
return super.onCreatePanelMenu(featureId, menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(this, "onMenuItemSelected", Toast.LENGTH_LONG).show();
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
Toast.makeText(this, "onMenuOpened", Toast.LENGTH_LONG).show();
return super.onMenuOpened(featureId, menu);
}
}
第三步、AndroidManifest.xml注册Activity
<activity android:name=".OptionsMenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步、运行效果
相关文章推荐
- Android开发之多个Activity跳转(Intent)及菜单用法(menu)
- android开发步步为营之26:进度条对话框ProgressDialog的用法
- android开发步步为营之63:webview常用用法
- android开发步步为营之115:HandlerThread的简单用法
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
- 第43章、选项菜单OptionsMenu(从零开始学Android)
- 【Android】选项菜单optionsMenu
- android开发步步为营之64:PopupWindow实现自定义弹出菜单
- 步步为营_Android开发课[20]_用户界面之Menu(菜单)
- 【Android】选项菜单optionsMenu
- Android开发:Menu选项菜单
- android开发步步为营之40:AsyncTask的用法
- android开发步步为营之27:上下文菜单ContextMenu的用法
- Android入门(44)——第六章 使用OptionsMenu实现选项菜单
- 【Android 开发教程】选项菜单
- android开发步步为营之17:MideaPlayer的用法
- android开发步步为营之35:GridView的用法
- 11. android 选项菜单(此处为icon menu)--Menu,SubMenu,MenuItem的用法
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
- Android选项菜单用法实例分析