Menu of Android learning notes

Source: Internet
Author: User

1. Normal Menu
Let's take a look at how to implement the simplest Menu.
Override the onCreateOptionsMenu (Menu menu) method in the main Activity.
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// TODO Auto-generated method stub
// Menu. add (0, 1, R. string. exit );
// Menu. add (0, 2, R. string. about );
Menu. add (0, 1, 1, "apple ");
Menu. add (0, 2, 2, "banana ");
Return super. onCreateOptionsMenu (menu );
}
 
 
In this way, two menu options are available. To add a click event, override the onOptionsItemSelected (MenuItem item) method.
 
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
If (item. getItemId () = 1 ){
Toast t = Toast. makeText (this, "you selected Apple", Toast. LENGTH_SHORT );
T. show ();
}
Else if (item. getItemId () = 2 ){
Toast t = Toast. makeText (this, "you selected bananas", Toast. LENGTH_SHORT );
T. show ();
}
Return super. onOptionsItemSelected (item );
}
 
 
Click Apple MenuItem
 
2 SubMenu
SubMenu creation is also simple. Add a few sentences to the onCreateOptionsMenu (Menu menu) method of the first code, as follows:
 
@ Override
Ublic boolean onCreateOptionsMenu (Menu menu ){
// TODO Auto-generated method stub
Menu. add (0, 1, 1, "apple ");
Menu. add (0, 2, 2, "banana ");
SubMenu subMenu = menu. addSubMenu (1,100,100, "Peach ");
SubMenu. add (2,101,101, "Peach ");
SubMenu. add (2,102,102, "Peach ");
Return super. onCreateOptionsMenu (menu );
 
 
After you click "Peach", the sub menu will appear. There are two sub options: "big peach" and "little peach ".





From sunset hut

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.