Menus are frequently used in Android development. Generally, there are two ways to create a Menu. One is to create a Menu by pressing the Menu button of the machine, and the other is to create a Menu by using OnCreateContextMenu. The following describes the specific implementation methods of the two methods.
<1> when you create a Menu by pressing the Menu button on the machine, you must override the onCreateOptionsMenu, onOptionsItemSelected, and onPrepareOptionsMenu methods in the Activity. You can use the former to create a Menu you need.
The following code creates a menu as needed:
1 @ Override
2 public boolean onCreateOptionsMenu (Menu menu ){
3 menu. add (0, 1, 1, "exit"). setIcon (android. R. drawable. ic_delete); // you can set the image of menu options.
4 menu. add (0, 2, 1, "about"); // groupId, itemId, order, title
5 menu. add (1, 3, 2, "help ");
6 menu. add (1, 4, 2, "send ");
7 menu. add (2, 5, 2, "call ");
8 menu. add (2, 6, 1, "tom ");
9 menu. add (2, 7, 1, "john ");
10 return super. onCreateOptionsMenu (menu );
11}
The running interface is as follows:
When the menu you created contains more than six projects, a "more" button will be used to display them because they cannot be fully displayed. OnPrepareOptionsMenu is used to manage the menu after the menu is created. Here we can set a menu item unavailable, alternatively, you can use the groupId set during menu creation to manage the same set of menu items.
Based on your business needs, you need to use onOptionsItemSelected to implement the functions of each project in the menu you create. The following is a simple function implemented by myself. Here we only want to explain how to use this method. Therefore, it is easy to write.
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Int id = item. getItemId ();
Intent intent = new Intent ();
Switch (id ){
Case 1:
Finish (); // exit
Break;
Case 2:
Toast. makeText (TestMenuActivity. this, "this is a joke! ",
Toast. LENGTH_LONG). show (); // display toast
Break;
Case 4:
Intent. setClass (TestMenuActivity. this, OtherActivity. class );
TestMenuActivity. this. startActivity (intent); // jump to another page
Break;
Default:
Break;
}
Return super. onOptionsItemSelected (item );
}
<2> when we do not want to create a Menu with the Menu provided by the machine itself, we can use any control in the Activity to create a listener to implement the Menu creation function. Here we will use the onCreateContextMenu and onContextItemSelected methods. SetOnCreateContextMenuListener is used to create a listener.
When you use the onCreateContextMenu method to create a Menu, there are two methods available here. One is to create a Menu folder in res and create an xml file in this folder, implement a menu (<menu> </menu>) by designing xml ). Then use MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. xml file name, menu) in the method to reference this menu. The other method is to directly add using the add method in this method. Below is a simple code for creating a menu to illustrate the use of the two methods:
Code in Activity:
1 findViewById (R. id. textView). setOnCreateContextMenuListener (new View. OnCreateContextMenuListener (){
2
3 @ Override
4 public void onCreateContextMenu (ContextMenu menu, View v,
5 ContextMenuInfo menuInfo ){
6 MenuInflater inflater = getMenuInflater ();
7 inflater. inflate (R. menu. menu1, menu );
8 // menu. add (0, 1, 1, "tom ");
9}
10 });
Here, I create a Menu folder in res and a menu. xml file in this folder.
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Group android: checkableBehavior = "single">
<Item android: id = "@ + id/new_game" android: title = "New Game"/>
<Item android: id = "@ + id/quit" android: title = "Quit"/>
</Group>
</Menu>
We can create a menu through the above. The following figure shows the running effect interface:
In addition, when we override the onCreateContextMenu method, we directly use menu to add menus. Here I will not explain how to do it. The result is the same as the preceding one.
After creating an external menu, we still need to implement the function of the menu. Here we need to override the onContextItemSelected method. Below is a simple code that I wrote to illustrate the usage of this method for your reference.
1 @ Override
2 public boolean onContextItemSelected (MenuItem item ){
3 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
4. getMenuInfo ();
5 Intent intent = new Intent ();
6 switch (item. getItemId ()){
7 case R. id. new_game:
8 intent. setClass (TestMenuActivity. this, OtherActivity. class );
9 TestMenuActivity. this. startActivity (intent); // jump to Activity
10/* Toast. makeText (TestMenuActivity. this, "this is a new_game ",
11 Toast. LENGTH_LONG). show ();*/
12 return true;
13 case R. id. quit:
14 finish (); // end and exit
15 return true;
16 default:
17 return super. onContextItemSelected (item );
18}
19}
In this way, we implement a menu in two ways. Menus are very useful for implementing a business requirement. We hope you can master these two simple menu creation methods.