Android pop-up menu and android pop-up menu
<! -- Define basic layout LinearLayout --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/activity_horizontal_margin" android: paddin GTop = "@ dimen/activity_vertical_margin" android: orientation = "vertical"> <! -- Define the TextView control --> <TextView android: layout_width = "match_parent" android: layout_height = "match_parent" android: text = "press the keyboard physical menu key, pop-up option menu "/> </LinearLayout>
Package com. example. yanlei. yl2; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. util. log; import android. view. menu; import android. view. menuItem; import android. widget. toast; // import the required package public class MainActivity extends AppCompatActivity {// define the Id private final static int Menu_1 = Menu for each Menu option in the Menu. FIRST; private final static int Menu_2 = Menu. FIRST + 1; private final static int Menu_3 = Menu. FIRST + 2; private final static int Menu_4 = Menu. FIRST + 3; private final static int Menu_5 = Menu. FIRST + 4; private final static int Menu_6 = Menu. FIRST + 5; private final static int Menu_7 = Menu. FIRST + 6; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // call the onCreate method of the parent class // set the layout file of the current page to activity_main setContentView (R. layout. activity_main);} // create the Menu callback method public boolean onCreateOptionsMenu (Menu m) {// The Parameter m is the current Activity menu object obtained. // if you want to add a method to the current page, add it. // the parameter of the add method: add (group id, itemid, sort, menu text) m. add (0, Menu_1, 0, "edit mode"); m. add (0, Menu_2, 0, "Modify Wallpaper"); m. add (0, Menu_3, 0, "global search"); m. add (0, Menu_4, 0, "desktop thumbnail"); m. add (0, Menu_5, 0, "desktop effect"); m. add (0, Menu_6, 0, "system settings"); m. add (0, Menu_7, 0, "user information"); return super. onCreateOptionsMenu (m);} // The callback event public boolean onOptionsItemSelected (MenuItem item) selected by the Menu option) {// The parameter is the selected menu option object // execute the corresponding function switch (item. getItemId () {case 1: Toast. makeText (this, "you clicked edit mode option", Toast. LENGTH_SHORT ). show (); break; case 2: Toast. makeText (this, "you have clicked modify Wallpaper", Toast. LENGTH_SHORT ). show (); break; case 3: Toast. makeText (this, "you clicked global search", Toast. LENGTH_SHORT ). show (); break; case 4: Toast. makeText (this, "you clicked the desktop thumbnail", Toast. LENGTH_SHORT ). show (); break; case 5: Toast. makeText (this, "you clicked the desktop effect", Toast. LENGTH_SHORT ). show (); break; case 6: Toast. makeText (this, "you clicked system settings", Toast. LENGTH_SHORT ). show (); break; case 7: Toast. makeText (this, "you clicked user information", Toast. LENGTH_SHORT ). show (); break;} return super. onOptionsItemSelected (item);} // The callback method public void onOptionsMenuClosed (Menu menu) {Log. e ("onOptionsMenuClosed", "user Menu disabled");} // The callback method public boolean onPrepareOptionsMenu (menu Menu menu) {Log. e ("onPrepareOptionsMenu", "user Menu ready to be displayed"); // if the return value is true, the Menu is displayed; otherwise, the return true is not displayed ;}}