歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android控件之Menu的實現

Android控件之Menu的實現

日期:2017/3/1 10:56:13   编辑:Linux編程

今天學習Menu控件比較簡單,直接上代碼了。首先是布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView android:layout_width="fill_parent"
  8. android:text="Menu測試"
  9. android:textSize="24dp"
  10. android:layout_height="wrap_content"></TextView>
  11. </LinearLayout>
下面是主程序代碼:
  1. package com.cloay;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.Toast;
  13. /**
  14. * MenuActivity.java
  15. * @author cloay
  16. * 2011-9-13
  17. */
  18. public class MenuActivity extends Activity {
  19. /** Called when the activity is first created. */
  20. NotificationManager notificationManager;
  21. Intent intent;
  22. PendingIntent pendindIntent;
  23. Notification notification;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  29. notification = new Notification();
  30. }
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu) {
  33. // TODO Auto-generated method stub
  34. /*
  35. *
  36. * add()方法的四個參數,依次是:
  37. *
  38. * 1、組別,如果不分組的話就寫Menu.NONE,
  39. *
  40. * 2、Id,這個很重要,Android根據這個Id來確定不同的菜單
  41. *
  42. * 3、順序,那個菜單現在在前面由這個參數的大小決定
  43. *
  44. * 4、文本,菜單的顯示文本
  45. */
  46. //圖標文件實現android系統自帶的文件
  47. menu.add(Menu.NONE, Menu.FIRST + 1, 1, "保存").setIcon(android.R.drawable.ic_menu_save);
  48. menu.add(Menu.NONE, Menu.FIRST + 2, 2, "添加").setIcon(android.R.drawable.ic_menu_add);
  49. menu.add(Menu.NONE, Menu.FIRST + 3, 3, "刪除").setIcon(android.R.drawable.ic_menu_delete);
  50. menu.add(Menu.NONE, Menu.FIRST + 4, 4, "發送").setIcon(android.R.drawable.ic_menu_send);
  51. menu.add(Menu.NONE, Menu.FIRST + 5, 5, "幫助").setIcon(android.R.drawable.ic_menu_help);
  52. menu.add(Menu.NONE, Menu.FIRST + 6, 6, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
  53. return super.onCreateOptionsMenu(menu);
  54. }
  55. @Override
  56. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  57. // TODO Auto-generated method stub
  58. switch(item.getItemId()){
  59. case Menu.FIRST + 1:
  60. Toast.makeText(MenuActivity.this, "保存菜單被點擊了!", Toast.LENGTH_LONG).show();
  61. //intent = new Intent(MenuActivity.this,Myhandler.class);
  62. //startActivity(intent);
  63. //overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  64. break;
  65. case Menu.FIRST + 2:
  66. Toast.makeText(MenuActivity.this, "添加菜單被點擊了!", Toast.LENGTH_LONG).show();
  67. break;
  68. case Menu.FIRST + 3:
  69. Toast.makeText(MenuActivity.this, "刪除菜單被點擊了!", Toast.LENGTH_LONG).show();
  70. break;
  71. case Menu.FIRST + 4:
  72. Toast.makeText(MenuActivity.this, "發送菜單被點擊了!", Toast.LENGTH_LONG).show();
  73. //通知在狀態欄顯示的圖標
  74. //notification.icon = android.R.drawable.ic_lock_silent_mode_off;
  75. //通知的內容
  76. //notification.tickerText = "發送菜單被點擊了!";
  77. //通知時發出的聲音
  78. //notification.defaults = Notification.DEFAULT_SOUND;
  79. //intent = new Intent(MenuActivity.this,mNotification.class);
  80. //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
  81. //pendindIntent = PendingIntent.getActivity(MenuActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  82. //notification.setLatestEventInfo(MenuActivity.this, "按鈕", "發送按鈕", pendindIntent);
  83. //notificationManager.notify(913, notification);
  84. break;
  85. case Menu.FIRST + 5:
  86. Toast.makeText(MenuActivity.this, "幫助菜單被點擊了!", Toast.LENGTH_LONG).show();
  87. break;
  88. case Menu.FIRST + 6:
  89. AlertDialog alertDialog = new AlertDialog.Builder(MenuActivity.this)
  90. .setTitle("提示!")
  91. .setIcon(R.drawable.ask)
  92. .setMessage("您確定要退出系統嗎?")
  93. .setPositiveButton("確定",
  94. new DialogInterface.OnClickListener() {
  95. @Override
  96. public void onClick(DialogInterface dialog, int which) {
  97. System.exit(0);
  98. dialog.cancel(); //提示對話框關閉
  99. }
  100. })
  101. .setNegativeButton("取消",
  102. new DialogInterface.OnClickListener() {
  103. @Override
  104. public void onClick(DialogInterface dialog, int which) {
  105. // TODO Auto-generated method stub
  106. dialog.cancel(); //關閉對話框
  107. }
  108. }).create();
  109. alertDialog.show();
  110. break;
  111. }
  112. return super.onMenuItemSelected(featureId, item);
  113. }
  114. }

運行效果如下:

Copyright © Linux教程網 All Rights Reserved