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. java、myeclipse常见错误的解决(未完)

    1.报错java.lang.NoClassDefFoundError ,但是相关jar包已经导入工程. 解决方案:将jar包放入C盘tomcat上部署的相应项目中的WEB-INF/lib中.web容器 ...

  2. NOR型flash与NAND型flash的区别

    1) 闪存芯片读写的基本单位不同  应用程序对NOR芯片操作以“字”为基本单位.为了方便对大容量NOR闪存的管理,通常将NOR闪存分成大小为128KB或者64KB的逻辑块,有时候块内还分成扇区.读写时 ...

  3. 类内const static(static const)成员变量初始化问题

    在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...

  4. bzoj 1432 [ZJOI2009]Function(找规律)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1432 [思路] 找(cha)规(ti)律(jie) 分析戳这儿 click here ...

  5. 45种Javascript技巧大全(转)

      6.小心使用typeof.instanceof和constructor typeof:不要忘了typeof null返回object,而大多数对象,typeof(Array, Date, and ...

  6. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  7. HDU-2686 Matrix 多进程DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 经典的多进程DP,比较简单.f[x1][y1][x2][y2]表示起点到点(x1,y1)和(x2 ...

  8. [思维题]Bored Qishen

    给出一个整数集,其中包含1-n的所有整数,要求挑选出一个元素最多的子集,使得子集中任意两数的乘积不是完全平方数 (n<=10^6) 求这样一个最大子集的元素个数 #include <cst ...

  9. hdoj 2099 整除的尾数

    整除的尾数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. 第3组UI组件:AdapterView及其子类

    1 AdapterView类简介 1.1 AdapterView组件是一组重要的组件,AdapterView本身是一个抽线类,实际使用更多的都是Adapter相关子类,AdapterView具有如下特 ...