Android Rewrite menu adds system comes with return key

Source: Internet
Author: User

Condition: Current Project Imports Actionbarsherlock this jar package, the role of this jar for program compatibility, consider the issue of the low version.

Learn actionbarsherlock Reference blog Link: http://blog.csdn.net/icyfox_bupt/article/details/9286387

Then put the code on it.

  1. @Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. Super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.activity_main);
  5. This line has to be written.
  6. Getsupportactionbar (). Setdisplayhomeasupenabled (True);
  7. }
  8. @Override
  9. public boolean Oncreateoptionsmenu (Menu menu) {
  10. return true;
  11. }
  12. Listen Back button action Event
  13. @Override
  14. public boolean onoptionsitemselected (MenuItem item) {
  15. Switch (Item.getitemid ()) {
  16. Case Android. R.id.home:
  17. Toast.maketext (This, "You clicked the Back button", Toast.length_long). Show ();
  18. return true;
  19. }
  20. return super.onoptionsitemselected (item);
  21. }

As follows:

If not introduced Actionbarsherlock this jar package is written as follows

  1. @Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. Super.oncreate (savedinstancestate);
  4. Getactionbar (). Setdisplayhomeasupenabled (true);//This sentence must be written
  5. }
  6. @Override
  7. public boolean onoptionsitemselected (MenuItem item) {
  8. Switch (Item.getitemid ()) {
  9. Case Android. r.id.home://Add Click events
  10. Finish ();
  11. Break
  12. Default
  13. Break
  14. }
  15. return super.onoptionsitemselected (item);
  16. }



By the way, several actionbar methods are introduced.

actionbar.setdisplayhomeasupenabled (flag); Controls whether the back button in the upper-left corner is displayed

actionbar.setdisplayshowhomeenabled (flag);//control the upper left corner returns whether the icon to the right of the button can be displayed

actionbar.sethomebuttonenabled (flag);//The upper left corner returns whether the button can be clicked

actionbar.setdisplayoptions (int); You can set multiple

The parameter is the value of some constants inside the Actionbar, and the official API is as follows:

Android Rewrite menu adds system comes with return key

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.