Android_menu_optionMenu
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的更多相关文章
随机推荐
- HDU 1074 Doing Homework 状压DP
由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...
- eclipse 远程wifi调试android程序
[原文]http://leanote.com/blog/view/541f8b2dbda4e44f75000000 下载wifidebug http://pan.baidu.com/s/1i3stnF ...
- C++中Const说明
最近在看程序员面试宝典,看到const这块感觉有很大疑惑,查了很多资料,可以总结如下: 1.在C语言中 在C语言中,const修饰的变量不具有常量的特性,只是一个不可修改的变量,实质上仍然是变量,在编 ...
- JDK1.5新特性(六)……Generics
概述 Generics - This long-awaited enhancement to the type system allows a type or method to operate on ...
- NOIP2009 最优贸易
3. 最优贸易 (trade.pas/c/cpp) [问题描述] C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间 多只有一条道路直接相连.这 m 条道 ...
- HW7.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Eclipse关联JavaDoc和源代码
1.Eclipse 关联 JavaDoc 1.在 Eclipse 菜单中点击 Window -> Preferences,在弹出框中左侧选择展开 Java 节点,点击 Installed JRE ...
- [Spice-devel] usbredir for Windows Client
Hello, I have been scouring the internet for information on how to do this. I've successfully instal ...
- Android uiautomator实例使用
转载自:http://blog.csdn.net/huiguixian/article/details/22398193 Android测试工具中,Monkey Runner只要简单几个指令即可,但他 ...
- delphi 调用 webservice (.NET C#版)
uses XMLIntf, XMLDoc; XML to XTR文件转换 .File-->open打开你要分析的XML文件 .在左边选择你要分析的接点,双击加到中间的转换列表中 .Create- ...