第一章:初入Android大门(添加Menu菜单)

效果:


[img]http://dl.iteye.com/upload/attachment/383594/208ff16c-e46b-335b-9a69-507f8ef73096.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/383597/1ba47376-cf0f-3938-99b4-d3a4f324bff0.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/383599/ebaa3a03-11e7-32c8-9a0b-7984fe8bb413.jpg[/img]


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();
}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值