Getting started with Android (10)-menu function menu design

Source: Internet
Author: User
Hello everyone, today we are talking about the design of the android function menu, Program Two menu items are defined, one is "about" and the other is "exit". When you click "about", a new one is created. Toast Prompt: When you click "exit", we will end the program.

In addition toOncreateIn addition, you also need to create two other class functions:Oncreateoptionsmenu() AndOnoptionsitemselected(), The former is the creationMenuThe 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,CodeAs 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.