1 package com.example.day11_actionbardemo; 2 3 import android.os.Bundle; 4 import android.app.ActionBar; 5 import android.app.ActionBar.OnNavigationListener; 6 import android.app.FragmentTransaction; 7 import android.app.ActionBar.Tab; 8 import android.app.ActionBar.TabListener; 9 import android.app.Activity; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.widget.ArrayAdapter; 13 import android.widget.Toast; 14 15 public class MainActivity extends Activity { 16 ActionBar bar; 17 18 String[] phone = {"未接电话","已接电话","已拨电话"}; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 init(); 25 } 26 //11111111111111111111111111111111111 27 @Override 28 public boolean onMenuItemSelected(int featureId, MenuItem item) { 29 // TODO Auto-generated method stub 30 int id = item.getItemId(); 31 if(id == android.R.id.home){ 32 this.finish(); 33 } 34 35 return super.onMenuItemSelected(featureId, item); 36 } 37 38 public void init(){ 39 bar = getActionBar();//获得actionbar 40 // bar.setDisplayShowHomeEnabled(false); 41 // bar.setDisplayShowTitleEnabled(false); 42 bar.setDisplayHomeAsUpEnabled(true);//激活图标的功能 43 44 //设置actionbar的样式,,,tab样式 45 // bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置为tab页的样式 46 // bar.setTitle("选项卡"); 47 // //添加选项卡(功能项) 48 // bar.addTab(bar.newTab().setText("未接电话").setIcon(R.drawable.ic_launcher).setTabListener(l)); 49 // bar.addTab(bar.newTab().setText("已接电话").setIcon(R.drawable.ic_launcher).setTabListener(l)); 50 // bar.addTab(bar.newTab().setText("已拨电话").setIcon(R.drawable.ic_launcher).setTabListener(l)); 51 52 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);//设置为list列表样式 53 //创建适配器,配置导航项 54 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, phone); 55 //配置数据,并添加监听 56 bar.setListNavigationCallbacks(adapter, new OnNavigationListener() { 57 58 @Override 59 public boolean onNavigationItemSelected(int itemPosition, long itemId) { 60 // TODO Auto-generated method stub 61 Toast.makeText(MainActivity.this, "您当前选中的是:"+phone[itemPosition], 0).show(); 62 if(phone[itemPosition].equals("未接电话")){ 63 64 }else if(phone[itemPosition].equals("已接电话")){ 65 66 } 67 return false; 68 } 69 }); 70 71 } 72 73 TabListener l = new TabListener() { 74 75 @Override 76 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 77 // TODO Auto-generated method stub 78 79 } 80 81 @Override 82 public void onTabSelected(Tab tab, FragmentTransaction ft) { 83 // TODO Auto-generated method stub 84 Toast.makeText(MainActivity.this, tab.getText(), 0).show(); 85 if(tab.getText().equals("未接电话")){ 86 87 }else if(tab.getText().equals("已接电话")){ 88 89 } 90 91 } 92 @Override 93 public void onTabReselected(Tab tab, FragmentTransaction ft) { 94 // TODO Auto-generated method stub 95 96 } 97 }; 98 99 @Override 100 public boolean onCreateOptionsMenu(Menu menu) { 101 // Inflate the menu; this adds items to the action bar if it is present. 102 getMenuInflater().inflate(R.menu.activity_main, menu); 103 return true; 104 } 105 106 }