Activity返回按钮

如图:

 

 1 package com.maomao.beautymovie;
 2 
 3 import android.app.ActionBar;
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.support.v4.app.FragmentActivity;
 7 import android.view.KeyEvent;
 8 import android.view.MenuItem;
 9 
10 
11 /**
12  * 
13  * @author NINGXUEFENG
14  *
15  */
16 public class BaseActivity extends Activity {
17 
18     protected ActionBar mActionBar;
19 
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         initActionBar();
23     }
24 
25     private void initActionBar() {
26         mActionBar = getActionBar();
27         mActionBar.setDisplayHomeAsUpEnabled(true);             // 给左上角图标的左边加上一个返回的图标"《" 。对应ActionBar.DISPLAY_HOME_AS_UP
28         mActionBar.setDisplayShowHomeEnabled(true);             //使左上角图标可点击,对应id为android.R.id.home,对应ActionBar.DISPLAY_SHOW_HOME false 则图标无法点击
29         mActionBar.setHomeButtonEnabled(true);                  // false 就无法点击
30     }
31 
32     public void setTitle(int resId) {
33         mActionBar.setTitle(resId);
34     }
35 
36     public void setTitle(CharSequence text) {
37         mActionBar.setTitle(text);
38     }
39 
40     @Override
41     public boolean onOptionsItemSelected(MenuItem item) {
42         switch (item.getItemId()) {
43               case android.R.id.home:
44                   onBackPressed();
45                   return true;
46             default:
47                 return super.onOptionsItemSelected(item);
48         }
49     }
50 
51 
52 }

 

posted @ 2015-03-30 14:53  will_N  阅读(669)  评论(0编辑  收藏  举报