今天學習Menu控件比較簡單,直接上代碼了。首先是布局文件:
- <?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:text="Menu測試"
- android:textSize="24dp"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
下面是主程序代碼:
- package com.cloay;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
-
- /**
- * MenuActivity.java
- * @author cloay
- * 2011-9-13
- */
- public class MenuActivity extends Activity {
- /** Called when the activity is first created. */
- NotificationManager notificationManager;
- Intent intent;
- PendingIntent pendindIntent;
- Notification notification;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- notification = new Notification();
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- /*
- *
- * add()方法的四個參數,依次是:
- *
- * 1、組別,如果不分組的話就寫Menu.NONE,
- *
- * 2、Id,這個很重要,Android根據這個Id來確定不同的菜單
- *
- * 3、順序,那個菜單現在在前面由這個參數的大小決定
- *
- * 4、文本,菜單的顯示文本
- */
- //圖標文件實現android系統自帶的文件
- menu.add(Menu.NONE, Menu.FIRST + 1, 1, "保存").setIcon(android.R.drawable.ic_menu_save);
- menu.add(Menu.NONE, Menu.FIRST + 2, 2, "添加").setIcon(android.R.drawable.ic_menu_add);
- menu.add(Menu.NONE, Menu.FIRST + 3, 3, "刪除").setIcon(android.R.drawable.ic_menu_delete);
- menu.add(Menu.NONE, Menu.FIRST + 4, 4, "發送").setIcon(android.R.drawable.ic_menu_send);
- menu.add(Menu.NONE, Menu.FIRST + 5, 5, "幫助").setIcon(android.R.drawable.ic_menu_help);
- menu.add(Menu.NONE, Menu.FIRST + 6, 6, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- // TODO Auto-generated method stub
- switch(item.getItemId()){
- case Menu.FIRST + 1:
- Toast.makeText(MenuActivity.this, "保存菜單被點擊了!", Toast.LENGTH_LONG).show();
- //intent = new Intent(MenuActivity.this,Myhandler.class);
- //startActivity(intent);
- //overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
- break;
- case Menu.FIRST + 2:
- Toast.makeText(MenuActivity.this, "添加菜單被點擊了!", Toast.LENGTH_LONG).show();
- break;
- case Menu.FIRST + 3:
- Toast.makeText(MenuActivity.this, "刪除菜單被點擊了!", Toast.LENGTH_LONG).show();
- break;
- case Menu.FIRST + 4:
- Toast.makeText(MenuActivity.this, "發送菜單被點擊了!", Toast.LENGTH_LONG).show();
- //通知在狀態欄顯示的圖標
- //notification.icon = android.R.drawable.ic_lock_silent_mode_off;
- //通知的內容
- //notification.tickerText = "發送菜單被點擊了!";
- //通知時發出的聲音
- //notification.defaults = Notification.DEFAULT_SOUND;
- //intent = new Intent(MenuActivity.this,mNotification.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
- //pendindIntent = PendingIntent.getActivity(MenuActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
- //notification.setLatestEventInfo(MenuActivity.this, "按鈕", "發送按鈕", pendindIntent);
- //notificationManager.notify(913, notification);
- break;
- case Menu.FIRST + 5:
- Toast.makeText(MenuActivity.this, "幫助菜單被點擊了!", Toast.LENGTH_LONG).show();
- break;
- case Menu.FIRST + 6:
- AlertDialog alertDialog = new AlertDialog.Builder(MenuActivity.this)
- .setTitle("提示!")
- .setIcon(R.drawable.ask)
- .setMessage("您確定要退出系統嗎?")
- .setPositiveButton("確定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- System.exit(0);
- dialog.cancel(); //提示對話框關閉
- }
- })
- .setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel(); //關閉對話框
- }
- }).create();
- alertDialog.show();
- break;
- }
- return super.onMenuItemSelected(featureId, item);
- }
- }
運行效果如下: