Tuesday, June 26, 2012

Menu is a very common tool most commonly used three 
Menu, SubMenu and ContextMenu 



Menu 
First to join Menu is very simple and override onCreateOptionsMenu as long as the Activity inside can 
@Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub return super.onCreateOptionsMenu(menu); } 

A close look at this method, it will pass a the menu 
We just use this menu to increase our Item 
As long as the write 
 menu.add(groupId, itemId, order, title) 

Can see that the incoming parameter is the groupId, on your behalf that a Group 
Usually set for Menu.NONE 
The second parameter represents the Item Menu inside the first of several, starting at 0, so the Menu there is a constant in First 
Usually written Menu.First 
The third parameter is the order in which you put in Item 
If you are set to Menu.NONE on your behalf using the default, the system will help you arrange the order of the fourth parameter is the title you want to put 

Suppose you put more than six of the Item, the sixth one will be automatically written "more" after press more 
Will the rest of the Item List list. 
 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub for(int i=0; i<10 ; i++){ menu.add(Menu.NONE, Menu.FIRST+i, Menu.NONE, "Item "+Integer.toString(i+1)); } return super.onCreateOptionsMenu(menu); } 
You will see the following icon 


How to press the Menu Item action? 
Override onOptionsItemSelected this method can be 
  @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub return super.onOptionsItemSelected(item); } 
It will still pass your point under the Item, as long as you use Item.getId () 
Can handle the point of action, such as 
  @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "你按下Item"+Integer.toString(item.getItemId()), Toast.LENGTH_SHORT).show(); return super.onOptionsItemSelected(item); } 
You can display the following figure you point under Item 




SubMenu 
Menu is the same, except that it used in the processing become out of the List to let you choose again joined the Menu, 
First create the Menu section an Item 
And then the establishment of the Item, SubItem 
 SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.FIRST, Menu.NONE, "Item "+1); for(int i=1; i<10 ; i++){ subMenu.add(Menu.NONE, Menu.FIRST+i, Menu.NONE, "Item "+Integer.toString(i+1)); } 
This will pop the SubItem the List as shown below 


Context Menu 
Usually this is long according to the current screen will pop up the List 
To with SubMenu the same as first override onCreateContextMenu as Menu 
  @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); } 

And Item one by one by adding menu inside 
  @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub for(int i=0; i<10 ; i++){ menu.add(Menu.NONE, Menu.FIRST+i, Menu.NONE, "Item "+Integer.toString(i+1)); } super.onCreateContextMenu(menu, v, menuInfo); } 

Come back to the XML help Layout plus the name of 
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"  android:id="@+id/myLinearLayout" > </LinearLayout> 

And then Menu unlikely to be registered in the onCreate inside 
  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); registerForContextMenu(findViewById(R.id.myLinearLayout)); } 

So you can picture a long time out of the List 
As shown below 


If you want to deal with pressing the action as long as the override 
  @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub return super.onContextItemSelected(item); } 

So the Menu is probably explanation is completed 

Sample file Download 
http://uploadingit.com/file/6s1hdymeahiz50gx/MenuDemo.zip

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -