Use of Menu level 1 Menu and level 2 Menu of Android learning notes, and android learning notes

Source: Internet
Author: User

Use of Menu level 1 Menu and level 2 Menu of Android learning notes, and android learning notes

(1) No changes have been made to the layout file.

(2) the code in the main. xml file under the res -- menu directory is as follows:

<Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Level 1 menu --> <item android: id = "@ + id/file" android: title = "@ string/file"> <! -- Level 2 menu --> <menu> <item android: id = "@ + id/create" android: title = "@ string/create"> </item> <item android: id = "@ + id/open" android: title = "@ string/open"> </item> </menu> </item> <! -- Level 1 menu --> <item android: id = "@ + id/chioce" android: title = "@ string/chioce"> <! -- Level 2 menu --> <menu> <item android: id = "@ + id/create1" android: title = "@ string/create"> </item> <item android: id = "@ + id/open1" android: title = "@ string/open"> </item> </menu>

(3) In the string. xml file under the values directory:

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> Menu_submenu </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> <string name = "file"> file </string> <string name = "create"> New </string> <string name = "open"> open </string> <string name = "chioce"> options </string> </resources>

(3) The MainActivity. java code is as follows:

Package com. example. menu_submenu; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/** processing level-1 menu */@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. file: Toast. makeText (MainActivity. this, "Level 1 menu file .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. chioce: Toast. makeText (MainActivity. this, "Level 1 menu chioce .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item);}/** process level-2 menu */@ Overridepublic boolean onMenuItemSelected (int featureId, MenuItem item) {switch (item. getItemId () {case R. id. create1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open1: Toast. makeText (MainActivity. this, "Process Level 2 menu create1 .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. create: Toast. makeText (MainActivity. this, "Process Level 2 menu create .. ", Toast. LENGTH_SHORT ). show (); break; case R. id. open: Toast. makeText (MainActivity. this, "Process Level 2 menu open .. ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onMenuItemSelected (featureId, item );}}


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.