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.
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- This line has to be written.
- Getsupportactionbar (). Setdisplayhomeasupenabled (True);
- }
- @Override
- public boolean Oncreateoptionsmenu (Menu menu) {
- return true;
- }
- Listen Back button action Event
- @Override
- public boolean onoptionsitemselected (MenuItem item) {
- Switch (Item.getitemid ()) {
- Case Android. R.id.home:
- Toast.maketext (This, "You clicked the Back button", Toast.length_long). Show ();
- return true;
- }
- return super.onoptionsitemselected (item);
- }
As follows:
If not introduced Actionbarsherlock this jar package is written as follows
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Getactionbar (). Setdisplayhomeasupenabled (true);//This sentence must be written
- }
- @Override
- public boolean onoptionsitemselected (MenuItem item) {
- Switch (Item.getitemid ()) {
- Case Android. r.id.home://Add Click events
- Finish ();
- Break
- Default
- Break
- }
- return super.onoptionsitemselected (item);
- }
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