xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.menudemo.OptionMenuDemo" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

源代码:

package com.example.menudemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
/**
* 选项菜单:optionMenu
*
* 创建选项菜单:重写onCreateMenu()
* |--设置菜单项
* 可用代码动态设置menu.add()
* 还可通过xml设置MenuInflater.inflate()
* |--设置菜单项点击事件:onOptionsItemSelected()
* @author Administrator
*
*/
public class OptionMenuDemo extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. //通过xml加载布局文件的形式创建菜单
//getMenuInflater().inflate(R.menu.main, menu); MenuItem item = menu.add(1, 1, 1, "menu1");
item.setTitle("高级 menu");
item.setIcon(R.drawable.ic_launcher);//api>=11不显示图标
menu.add(1, 2, 1, "menu2");
menu.add(1, 3, 1, "menu3");
menu.add(1, 4, 1, "menu4");
menu.add(1, 5, 1, "menu5");
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
/*case R.id.menu1:
Toast.makeText(this, "clicked menu1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "clicked menu2", Toast.LENGTH_SHORT).show();
break;*/
case 1:{
//点击菜单1跳转到第二个页面
Intent intent = new Intent(this,SecondActivity.class);
item.setIntent(intent);
Toast.makeText(this, "clicked menu1", Toast.LENGTH_SHORT).show();
break;
}
case 2:
Toast.makeText(this, "clicked menu2", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "clicked menu3", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "clicked menu4", Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this, "clicked menu5", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}

Android_menu_optionMenu的更多相关文章

随机推荐

  1. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.5

    Show that the inner product $$\bex \sef{x_1\vee \cdots \vee x_k,y_1\vee \cdots\vee y_k} \eex$$ is eq ...

  2. python引用在函数传参时的体现以及可变与不可变对象的对比

    今天偶然看到 vamei 老师的博客 http://www.cnblogs.com/vamei/archive/2012/07/10/2582795.html 讲的是python动态类型以及引用的事儿 ...

  3. 用通俗的例子解释OAuth和OpenID的区别【原】

    什么是OAuth(Wiki) 什么是OpenID(Wiki) 详细的定义可以看wiki,下面举个例子说说我的理解 现在很多网站都可以用第三方的账号登陆,比如,现在我要登录淘宝买东西,而如果我没有淘宝的 ...

  4. matlab 函数说明--waitforbuttonpress

    这个函数的名称取得不是太好,一眼看去,好像是等待按键的意思,但是实际上它等待的是按键或者鼠标事件,他的功能描述如下: 停止脚本的执行,直至当前活动的窗口上检测到了鼠标按下事件或者有效的键盘事件(有效是 ...

  5. jQuery遍历多层json数据

    1.json与jsonp的区别(待查) 2.要遍历的数据如下: {"status": "ok", "code": 200, "da ...

  6. CDH ecosystem components

    1,Mahout ASF(Apache Software Foundation)开源项目,提供可扩展的`机器学习`--(ML,Machine Learning多领域交叉学科,涉及概率,统计,逼近,凸分 ...

  7. 配置nginx,支持php的pathinfo路径模式

    nginx模式默认是不支持pathinfo模式的,类似index.php/index形式的url会被提示找不到页面.下面的通过正则找出实际文件路径和pathinfo部分的方法,让nginx支持path ...

  8. Install PhoneGap

    To Install, ensure that you have NodeJS installed, then open your commandline and run the following: ...

  9. [Objective-c 基础 - 2.9] 类的本质

    A.概念 类对象:类也是一个对象,是Class类型的对象 实例对象:创建的对象,有一个isa指针指向类   B.操作 获取内存中的内对象 1. 使用实例对象获取 Class c = [p class] ...

  10. (JAVA版)冒泡排序

    核心代码: public void bubbleSort(){ ;i<length-;i++){ ;j<length-i-;j++){ ]) swap(j,j+); } } } publi ...