第一章:初入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();
}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章