Menu in Android

Source: Internet
Author: User

In Android, menus can be divided into options menu (generated by the menu key) and context menu (generated by pressing the screen for a long time)

The following is an example;

1, Options menu (generated by the menu key ),

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {m=menu;menu.add(0, Menu.FIRST, 0, "1st");menu.add(0, Menu.FIRST+1, 0, "2st");menu.add(0, Menu.FIRST+2, 0, "3st");menu.add(0, Menu.FIRST+3, 0, "4st");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {  case 1:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;case 2:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;case 3:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;case 4:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;}return super.onOptionsItemSelected(item);}

Note: The public Boolean oncreateoptionsmenu (menu) method is called only once by the system. To change the menu content dynamically, You need to rewrite the onprepareoptionsmenu (menu) method.

Menu m=null;int count=0; @Overridepublic boolean onPrepareOptionsMenu(Menu menu) {if(count>0){if(count%2==0){menu.removeGroup(1);}else{menu.add(1, Menu.FIRST, 0, "5st");menu.add(1, Menu.FIRST+1, 0, "6st");}}count++;return super.onPrepareOptionsMenu(menu);}

2. Context menu (long-pressed screen)

@Overridepublic boolean onContextItemSelected(MenuItem item) {    switch (item.getItemId()) {      case 1:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;case 2:Toast.makeText(this, "you select"+item.getItemId(), 500).show();break;    }return super.onContextItemSelected(item);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {menu.add(0, Menu.FIRST, 0, "1st");menu.add(0, Menu.FIRST+1, 0, "2st");super.onCreateContextMenu(menu, v, menuInfo);}

Another step is to bind the view;

/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);     //   setContentView(R.layout.main);        MyView my=new MyView(this);        setContentView(my);        registerForContextMenu(my);    }

The above is implemented through code and can also be implemented through XML files

The menu. XML content is as follows;

<menu xmlns:android="http://schemas.android.com/apk/res/android">       <item android:id="@+id/fourth_item"        android:orderInCategory="3"        android:title="Fourth" />    <item android:id="@+id/third_item"        android:orderInCategory="2"        android:title="Third" />    <item android:id="@+id/dive"        android:orderInCategory="1"        android:title="Second" />    <item android:id="@+id/jump"        android:orderInCategory="0"        android:title="First" /></menu>

Load XML files;

@Override    public boolean onCreateOptionsMenu(Menu menu) {               // Inflate the currently selected menu XML resource.        MenuInflater inflater = getMenuInflater();        inflater.inflate(sMenuExampleResources[R.m], menu);                        return true;    } @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {                                          case R.id.jump:                Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();                return true;            case R.id.dive:                Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show();return false;}               

OK, all done, huh, huh...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.