I found an e-book "Android Application Framework principles and programming 36 skills". I personally think it is good. If you are interested, you can search it online. The code below is written along with the examples in the book, and some personal notes are added on this basis. I am also a beginner in Android. If there are any mistakes, I hope you can correct them.
If you are a beginner like me, it is recommended that you first look at the activity class and menu class documents to help you understand the following code. The specific document location is:
Activity Type android-sdk-windows-1.5_r3/docs/reference/Android/APP/activity.html
Menu class android-sdk-windows-1.5_r3/docs/reference/Android/View/menu.html
Preview:
The following is the ex01.java class:
Package COM. misoo; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. menu; <br/> Import android. view. menuitem; </P> <p> public class ex01 extends activity {<br/>/** specify the corresponding id value of each menu item to distinguish between them, make sure these values are different. */<br/> Public static final int add_id = menu. first; // Add command ID value <br/> Public static final int delete_id = menu. first + 1; // the ID of the DELETE command <br/> Public static final int exit_id = menu. first + 2; // corresponding id value of the exit command </P> <p>/** called when the activity is created for the first time */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); // according to main. <br/>}</P> <p>/** this method is called only when the menu is displayed for the first time */<br/> @ override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> Boolean B = super. oncreateoptionsmenu (menu); <br/>/** <br/> * menu. add (INT groupid, int Itemid, int order, charsequence title) <br/> * groupid is the group ID, which is not used here, so it is set to menu. none <br/> * Itemid is the item ID. When a command is clicked, the order of the clicked commands is different. Therefore, different IDs are set. <br/> * Order is the order, the smaller the value, the higher the value (cannot be a negative number). Here, the order is added, so it is also set to menu. none <br/> * title indicates the name of the command to be displayed, which is based on RES/values/strings. XML name to get the corresponding value <br/> * It is estimated that the processing is to change these values directly in XML in the future, you do not need to change the code <br/> */<br/> menu. add (menu. none, add_id, menu. none, R. string. menu_add); <br/> menu. add (menu. none, delete_id, menu. none, R. string. menu_delete); <br/> menu. add (menu. none, exit_id, menu. none, R. string. menu_exit); <br/> return B; // if the return value is not true, the menu is not displayed <br/>}</P> <p>/**. When the menu is displayed, this method is called. In this example, */<br/> @ override <br/> Public Boolean onprepareoptionsmenu (menu) {<br/> return Super. onprepareoptionsmenu (menu); <br/>}</P> <p>/** when the menu is disabled each time, will call this method. In this example, */<br/> @ override <br/> Public void onoptionsmenuclosed (menu) {<br/> super. onoptionsmenuclosed (menu); <br/>}</P> <p>/** when a menu has a command selected, this method is called */<br/> @ override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> switch (item. getitemid () {<br/> case add_id: <br/> settitle ("add"); // set the title to "add" <br/> break; </P> <p> case delete_id: <br/> settitle ("delete"); // set the title to "delete" <br/> break; </P> <p> case exit_id: <br/> finish (); // exit the Program <br/> break; <br/>}< br/> // return false to allow normal menus to process resources. If true is returned, destroy it directly. <br/> return Super. onoptionsitemselected (item); <br/>}</P> <p>}
The following figure shows Res/values/string. xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <string name = "hello"> Hello world, ex01! </String> <br/> <string name = "app_name"> ex01 </string> <br/> <string name = "menu_add"> Add item </string> <br/> <string name = "menu_delete"> Del item </string> <br/> <string name = "menu_exit"> exit </string> <br/> </ resources> <br/>