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的更多相关文章
随机推荐
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute th ...
- web.xml 配置的详解
http://my.oschina.net/u/1383439/blog/224448 http://blog.csdn.net/guihaijinfen/article/details/836383 ...
- QTP安装和破解
(1)底版本QTP安装破解 安装:http://bbs.51testing.com/thread-527025-1-1.html 进行破解: 1.创建文件夹C:/Program Files/Commo ...
- Android选项卡TabHost方式实现
1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...
- android NDK 实用学习(五)-c++端调用java接口
1,阅读此文章前请阅读前面文章,以免阅读出现障碍: android NDK 实用学习(一)-获取java端类及其类变量 android NDK 实用学习(二)-java端对象成员赋值和获取对象成员值 ...
- [读书笔记]了不起的node.js(二)
这周做项目做得比较散(应该说一直都是这样),总结就依据不同情境双开吧-这篇记录的是关于node的学习总结,而下一篇是做项目学到的web前端的知识. 1.HTTP篇 node的HTTP模块在第一篇时接触 ...
- Tcp/Ip协议族简单解读及网络数据包/报/帧数据格式及封装及解包;
http://www.creseek.cn/products-install/install_on_bsd_linux/ 中文检索 离线cloudera ecosystem components: h ...
- SQL2008-删除时间字段重复的方法
ID EMAgitation_ID YieldDateTime 1 2 2012-02-11 10:18:54.0002 2 ...
- A Tour of Go Maps
A map maps keys to values. Maps must be created with make (not new) before use; the nil map is empty ...
- [iOS基础控件 - 5.3] 电台APP可滚动主界面(UIScrollView制作)
A.功能 1.上下可滚动的电台 2 x n 的图标集 2.顶部半透明标题 3.底部半透明功能按钮 B.实现思路 1.设置图标.启动画面 2.拖入UIScrollView,设置背景色 ( ...