Basic Android tutorial (V)-menu function menu design

Source: Internet
Author: User

Hello everyone, today we are talking about the design of the android function menu. Two menu items are defined in the program. One is "about" and the other is "quit ", when you click "about", createToast
Prompt: When you click "exit", we will end the program.

In addition toOncreate
In addition, you also need to create two other class functions:Oncreateoptionsmenu
() AndOnoptionsitemselected
(), The former is the creationMenu
The menu item, which is used to process the event after the menu is selected for running.

Take a look:

 

We only made changes in one file, that isMenudemo. Java
The Code is as follows:

 

Package com. Android. test;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. widget. Toast;

Public class menudemo extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}

// Create a menu
 Public Boolean oncreateoptionsmenu (menu ){
Menu. Add (0, 0, 0, "about ");
Menu. Add (0, 1, 1, "quit ");
Return super. oncreateoptionsmenu (menu );
}


// Menu response
 Public Boolean onoptionsitemselected (menuitem item ){
Super. onoptionsitemselected (item );
Switch (item. getitemid ()){
Case 0:
Toast. maketext (menudemo. This, "Welcome to Wei zhulin's blog", Toast. length_long). Show ();
Case 1:
This. Finish ();
}
Return true;
}


}

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.