Implementation of Menu in android Control

Source: Internet
Author: User

Today, it is relatively simple to learn the menu control, and you can directly access the code. First, layout files:

<? 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 test" Android: textsize = "24dp" Android: layout_height = "wrap_content"> </textview> </linearlayout>

The main program code is as follows:

Package com. cloay; import android. app. activity; import android. app. alertDialog; import android. app. notification; import android. app. icationicationmanager; 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 * 20 11-9-13 */public class MenuActivity extends Activity {/** Called when the activity is first created. */icationicationmanager; Intent intent; PendingIntent pendindIntent; Notification notification; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); icationicationmanager = (notificationManager) getSystemService (NOTIFICATION_SERVICE); notification = new Notification () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// four parameters of the TODO Auto-generated method stub/*** add () method, in sequence: ** 1. group. If no group is selected, write Menu. NONE, ** 2, Id, which is very important. Android determines different menus based on this Id. ** 3. Order, the menu is now determined by the size of this parameter ** 4. text. The displayed text * // icon file of the menu implements the File menu that comes with the android system. add (Menu. NONE, Menu. FIRST + 1, 1, "save "). setIcon (android. r. drawable. ic_menu_save); men U. add (Menu. NONE, Menu. FIRST + 2, 2, "add "). setIcon (android. r. drawable. ic_menu_add); menu. add (Menu. NONE, Menu. FIRST + 3, 3, "delete "). setIcon (android. r. drawable. ic_menu_delete); menu. add (Menu. NONE, Menu. FIRST + 4, 4, "send "). setIcon (android. r. drawable. ic_menu_send); menu. add (Menu. NONE, Menu. FIRST + 5, 5, "help "). setIcon (android. r. drawable. ic_menu_help); menu. add (Menu. NONE, Menu. FIRST + 6, 6, "quit "). setIcon (andro Id. r. drawable. ic_menu_close_clear_cancel); return super. onCreateOptionsMenu (menu) ;}@ Overridepublic boolean onMenuItemSelected (int featureId, MenuItem item) {// TODO Auto-generated method stubswitch (item. getItemId () {case Menu. FIRST + 1: Toast. makeText (MenuActivity. this, "the Save menu has been clicked! ", 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, "The add menu has been clicked! ", Toast. LENGTH_LONG). show (); break; case Menu. FIRST + 3: Toast. makeText (MenuActivity. this," the delete Menu has been clicked! ", Toast. LENGTH_LONG). show (); break; case Menu. FIRST + 4: Toast. makeText (MenuActivity. this," the sending Menu has been clicked! ", Toast. LENGTH_LONG ). show (); // the icon of the notification displayed in the status bar // notification. icon = android. r. drawable. ic_lock_silent_mode_off; // notification content // notification. tickerText = "the sending menu has been clicked! "; // The sound sent during the notification // 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, "button", "Send button", pendindIntent); // no TificationManager. Y (913, notification); break; case Menu. FIRST + 5: Toast. makeText (MenuActivity. this, "the Help Menu has been clicked! ", Toast. LENGTH_LONG). show (); break; case Menu. FIRST + 6: AlertDialog alertDialog = new AlertDialog. Builder (MenuActivity. this). setTitle (" prompt! "). SetIcon (R. drawable. ask). setMessage (" are you sure you want to exit the system? "). SetPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {System. exit (0); dialog. cancel (); // prompt to close the dialog box }}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog. cancel (); // close the dialog box }}). create (); alertDialog. show (); break;} return super. onMenuItemSelected (featureId, item );}}

The running effect is as follows:

Note: Please indicate the source for reprinting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.