Menu Control
You do not need to set the Menu style in the layout file. You only need to add and sort the Menu in the Override onCreateOptionsMenu (menu) method in the Activity. The method for adding items in the menu is add (int groupId, int itemId, int order, CharSequence title). Here there are four parameters. The first one is groupId. If it is set to a single groupId, the items in the menu are ordered by order, that is, by order of the third parameter. However, if there are two groupId, the display order of the menu is item1 of group1, item1 of group2, item2 of group1, item2 of group2 ...... Similarly, each group displays items in turn. The second parameter is the unique identifier of the Item and must be inconsistent with each other. The third parameter is the display order of items in the group; the fourth parameter is the display name of the item.
The code snippet is as follows:
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// TODO Auto-generated method stub
Super. onCreateOptionsMenu (menu );
// Group1
Menu. add (1, 1, 1, R. string. aa );
Menu. add (1, 2, 2, R. string. bb );
Menu. add (1, 3, 3, R. string. cc );
// Group2
Menu. add (2, 4, 1, R. string. dd );
Menu. add (2, 5, 2, R. string. ee );
Menu. add (2, 6, 3, R. string. ff );
Return true;
}
Of course, there are other add methods. The above add method is the most detailed.
In this case, run the android Virtual Machine and click the menu button to obtain the following menus:
!!! Note the order of buttons.
Then, of course, you need to add some events to these buttons.
Here the Override onOptionsItemSelected (MenuItem item) method is required. The parameter item is the item we clicked.
In this function, we can use the switch or other condition judgment statement to process different events for different buttons. The following code snippets process events for A and F:
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// TODO Auto-generated method stub
Switch (item. getItemId ()){
Case 1:
Toast. makeText (ControlDemo2Activity. this,
String. valueOf (item. getItemId (), Toast. LENGTH_SHORT)
. Show (); // The itemId of the selected item is displayed.
Break;
Case 6:
Finish (); // close
Break;
}
Return super. onOptionsItemSelected (item );
}
Toast Control
The Toast control is a relatively simple control, which is also applied in the above example. The effect is that when an event is triggered or a prompt is required, a short message is displayed on the Activity.
The effect is as follows:
Toast implementation is very simple. You only need to use the Toast static method.
MakeText (Context context, int resId, int duration)
Or
MakeText (Context context, CharSequence text, int duration)
The first parameter of the two methods is the Context object, which is generally the Activity in which it is located. The third parameter is the duration of the Toast Pop-up. You can use Toast. LENGTH_LONG or Toast. you can also use the setDuration method to set the length. The difference is the third parameter, which references the content in the resource file, and the latter is the object that needs to be added with a CharSequence, which can be a String.
The code snippet is as follows:
Toast. makeText (ToastDemoActivity. this, R. string. toast, Toast. LENGTH_LONG). show ();
Of course, you can also set the Toast style by yourself. Note that you must call the static method makeText of Toast to set the style and content by yourself. The method is as follows:
Toast toast = Toast. makeText (ToastDemoActivity. this, "", Toast. LENGTH_LONG );
Toast. setDuration (0); // change the Toast Pop-up duration
Toast. setText ("aaa"); // change the Toast text.
Toast. show ();
Toast has many custom styles, including setGravity (), setMargin (), setXOffset (), and setYOffset (), which can be customized according to your preferences.
The attachment is the sample code for reference only.
This article is from the "Temple of war" blog