android 菜单例子

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

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debugging Scratch Pad"
/>
</LinearLayout>


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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xiaohang.zhimeng"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleMenusActivity"
android:label="Sample Menus Application">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="9" />

</manifest>


运行效果如下

[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]

源码附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值