也没什么了 就是一个 android菜单的 小例子 实现步骤如下:
(1)创建一个包含文本视图的XML布局文件。
(2)创建一个Activity类,其中包含在第一步中定义的布局。
(3)设置菜单
(4)向菜单添加一些常规菜单项
(5)向菜单添加一些辅助菜单项
(6)响应菜单项
(7)修改 AndroidManifest.xml文件,以显示应用程序正确的标题

布局文件
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debugging Scratch Pad"
/>



Activity类
package xiaohang.zhimeng;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SampleMenusActivity extends Activity {

Menu myMenu = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

//重写onCreateOptionsMenu 并以编程方式设置菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// call the parent to attach any system level menus
super.onCreateOptionsMenu(menu);
this.myMenu = menu;

//add a few normal menus
addRegularMenuItems(menu);

//add a few secondary menus
add5SecondaryMenuItems(menu);

//it must return true to show the menu
//if it is false menu won't show
return true;

}

//添加常规菜单项
private void addRegularMenuItems(Menu menu){
int base = Menu.FIRST; // value is 1

menu.add(base, base, base, "append");
menu.add(base, base+1, base+1, "item 2");
menu.add(base, base+2, base+2, "clear");

menu.add(base, base+3, base+3, "hide secondary");
menu.add(base, base+4, base+4, "show secondary");

menu.add(base, base+5, base+5, "enable secondary");
menu.add(base, base+6, base+6, "disable secondary");

menu.add(base, base+7, base+7, "check secondary");
menu.add(base, base+8, base+8, "uncheck secondary");
}

//添加辅助菜单项
private void add5SecondaryMenuItems(Menu menu){
//Secondary items are shown just like everything else
int base=Menu.CATEGORY_SECONDARY;

menu.add(base, base+1, base+1, "sec. item 1");
menu.add(base, base+2, base+2, "sec. item 2");
menu.add(base, base+3, base+3, "sec. item 3");
menu.add(base, base+4, base+4, "sec. item 4");
menu.add(base, base+5, base+5, "sec. item 5");

}

//响应菜单项单击
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
appendText("\nhello");
}
else if (item.getItemId() ==2) {
appendText("\nitem2");
}
else if (item.getItemId() == 3) {
emptyText();
}
else if (item.getItemId() ==4) {
//hide secondary
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
}
else if(item.getItemId() == 5){
//show secondary
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
}
else if (item.getItemId() == 6) {
//enable secondary
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
}
else if(item.getItemId() == 7){
//disable secondary
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
}
else if (item.getItemId() == 8) {
//check secondary
this.appendMenuItemText(item);
myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
}
else if (item.getItemId() == 9) {
//uncheck secondary
this.appendMenuItemText(item);
myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
}
else {
this.appendMenuItemText(item);
}
//should return true if the menu item
//is handled
return true;

}


//向调试TextView 写入数据的实用程序函数
//Given a string of text append it to the TextView
private void appendText(String text){
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText(tv.getText() + text);
}

//Given a menu item append its title to the TextView
private void appendMenuItemText(MenuItem menuItem){
String title = menuItem.getTitle().toString();
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText(tv.getText() + "\n" + title);
}

//Empty the TextView of its contents
private void emptyText(){
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText("");
}
}


AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
package="xiaohang.zhimeng"
android:versionCode="1"
android:versionName="1.0">

android:label="Sample Menus Application">











运行效果如下

[img]http://dl.iteye.com/upload/attachment/461776/80e59777-03a9-3bbd-b5a5-ab705ba38c10.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/461778/65821ac3-ab62-3e67-a2e9-3a6e05980154.jpg[/img]

源码附件

更多相关文章

  1. android 操作sdcard中的多媒体文件(一)——音乐列表的制作
  2. Android高手进阶教程(四)之----Android(安卓)中自定义属性(attr.
  3. Android双向侧边滑动实现
  4. android操作sdcard中的多媒体文件(一)——音乐列表的制作
  5. Android中的控件
  6. Android(安卓)ADB常用命令以及环境配置
  7. Android创建sdcard
  8. 编译android 之后生成的 img 文件介绍
  9. NPM 和webpack 的基础使用

随机推荐

  1. ubuntu修改根用户密码
  2. Linux利用i节点删除乱码文件
  3. linux暴露端口可以被外部访问
  4. 装完Centos7提示Initial setup of CentOS
  5. linux中使用nfs共享文件
  6. 在 Ubuntu 上安装 Neovim (nvim)
  7. 如何使用与相同文件运行的命令更新文件?
  8. Linux安装ElasticSearch-2.2.0-分词器插
  9. 解决useradd 用户后没有添加用户Home目录
  10. 【RFB】Linux uinput 分析,虚拟鼠标,键盘