效果:
main.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AboutMenuTest!</string>
<string name="meun_about">关于</string>
<string name="menn_exit">退出</string>
<string name="title">消息框</string>
<string name="about_text">这是个about对话框!</string>
<string name="enter">确定</string>
<string name="exit_text">您是否要退出?</string>
<string name="exit_cancle">取消</string>
<string name="app_name">AboutMenuTest</string>
</resources>
package about.menu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class AboutMenuTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** 载入main.xml文件 */
setContentView(R.layout.main);
}
/** 覆盖onCreateOptionsMenu函数添加两个menu菜单*/
public boolean onCreateOptionsMenu(Menu menu){
/**
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
参数
groupId 该组织标识符中应包含这个项目。这可能是用于定义组项批状态变化。如果一个项目没有通常使用不应当是一组。
itemid "唯一"物品的ID。如果你没有使用不需要有一个唯一的ID。
order 订条款。如果你没有使用不关心这个命令。看到getOrder()。
title 标题 的文字显示。
返回 新添加的菜单项。
*/
menu.add(0,0,0,R.string.meun_about);
menu.add(0,1,1,R.string.menn_exit);
return super.onCreateOptionsMenu(menu);
}
/** 覆盖onOptionsItemSelected函数判断用户选择的标题*/
public boolean onOptionsItemSelected(MenuItem menuItem){
/** 调用一次获得选择的按钮ID*/
super.onOptionsItemSelected(menuItem);
switch(menuItem.getItemId()){
case 0:
showAbout();
break;
case 1:
showIsExit();
break;
}
return true;
}
/** 弹出关于对话框*/
public void showAbout(){
/** 创建一个弹出对话框对象设置标题,消息体,按钮事件*/
new AlertDialog.Builder(this).setTitle(R.string.title).setMessage(R.string.about_text).setPositiveButton(R.string.enter,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
/** 弹出退出对话框*/
public void showIsExit(){
/** 创建一个弹出对话框对象设置标题,消息体,确定和取消按钮事件*/
new AlertDialog.Builder(this).setTitle(R.string.title).setMessage(R.string.exit_text).setNegativeButton(R.string.exit_cancle, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).setPositiveButton(R.string.enter, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
/** 退出程序*/
finish();
}
}).show();
}
}
- 大小: 6.6 KB
- 大小: 13 KB
- 大小: 13.4 KB
分享到:
相关推荐
android Menu菜单。android Menu菜单。android Menu菜单。android Menu菜单。
android 另类menu菜单提示android 另类menu菜单提示android 另类menu菜单提示android 另类menu菜单提示android 另类menu菜单提示
Android 按Menu弹出菜单,九宫格样式。
android menu菜单距中显示
Android 动态更新Menu菜单
android sliding menu 滑动菜单
android里使用menu,包括OptionMenu、ContextMenu、SubMenu,适合初学者,非常简单的代码,另外里面还附加了一个word文档教程,欢迎广大初学android的朋友下载使用!注释也很多的!
方法名 触发时间onCreateOptionsMenu 进入Launcher后第一次点MENU按钮时触发onPrepareOptionsMenu 有两种情况:1、进入Launcher后第一次点MENU按钮触发onCreateOptionsMenu后调用;2、经过第一步,以后每次点MENU...
CircleMenu for Android Check this library on other platforms: Looking for developers for your project? The Android mockup available here. Requirements Android 4.1 Jelly Bean (API lvl ...
Android DropDownMenu下拉菜单,漂亮的样式
android通过使用popupwindow控件实现自定义menu菜单
Android源码——menu单选菜单.zip
Android menu单选菜单.rar
Android ContextMenu实例,为EditText文本框添加上下文菜单,菜单激活并显示的方式是,用户在EditText输入框上面长按2秒,即可弹出上下文菜单,如运行截图所示的效果,为实现此功能,创建了onCreateContextMenu ...
menu单选菜单.zip
Android---UI篇---Menu(菜单), Android系统里面有3种类型的菜单:options menu,context menu,sub menu。
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu).参考博客:...
Android menu 安卓菜单实例教程
Android初级入门——菜单Menu的简单使用
android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu