android控件之Menu的实现

openkk 13年前

今天学习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);   }  }

运行效果如下:

android控件之Menu的实现

转自:http://blog.csdn.net/shang_515/article/details/6781246