Android menu event, androidmenu

Source: Internet
Author: User

Android menu event, androidmenu

@Override    public boolean onCreateOptionsMenu(Menu menu) {        menu.add(0,1,1,R.string.exit);        menu.add(0,2,2,R.string.about);        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case 1:                finish();                break;            case 2:                break;        }        return super.onOptionsItemSelected(item);    }


Android native development, how can we set ContextMenu to a non-Long-press event, with code attached

Public void onClick (View v) {super. onClick (v); this. openContextMenu (v );}

How can onOptionsItemSelected of android menu be associated with menu?

You can see that you created the menu in the xml format. In onOptionsItemSelected, switch {case (R. id. XXX)} corresponds to the click events of each menu. If it is code creation, it is the id you created.
For example, menu. add (0, 1, 1, "XXX"); this is the same in onOptionsItemSelected switch {
Case 1: // TODO (this is the click event of this menu item .) }

The menu method has been encapsulated in the parent class of Activity. You only need to override the onCreateOptionMenu () method of the parent class and onOptionsItemSelected.

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.