ActionBar实用案例(返回功能、子菜单、搜索功能)

转载请注明出处:http://blog.csdn.net/droyon/article/details/8309880

第一次写博客,不妥之处,敬请见谅。

前一段时间,在学习ApiDemos,写了一些总结,现在和大家分享一下。

 

案例效果图:

     

 

 

点击进入源代码下载页

 

实例源代码:

1、MainActivity.java

 

package com.example.actionbarcommomuse;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SearchView;
import android.widget.Switch;

@SuppressLint("NewApi")
 public class MainActivity extends Activity implements OnCheckedChangeListener{
 private final String TAG = "actionBar";
 private Context mContext = null;
 public Switch mSwitch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        setupActionBar();
    }
 public void setupActionBar(){
  mSwitch = new Switch(mContext);
  mSwitch.setChecked(false);
  mSwitch.setPadding(0, 0, 0, 0);
  getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
    ActionBar.DISPLAY_SHOW_CUSTOM);
  getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
    Gravity.CENTER_VERTICAL|Gravity.RIGHT));
  getActionBar().setDisplayHomeAsUpEnabled(true);
 }
    @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.activity_main, menu);
     MenuItem deleteItem = menu.add(Menu.NONE, 22,0, "delete");
  deleteItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  MenuItem searchItem = menu.findItem(R.id.search);
  SearchView sv = (SearchView) searchItem.getActionView();
  sv.setQueryHint(getString(R.string.hello_world));
  sv.setIconifiedByDefault(true);
  sv.setOnQueryTextListener(oQueryTextListener);
  return true;

 }
OnQueryTextListener oQueryTextListener = new OnQueryTextListener() {
  
  @Override
  public boolean onQueryTextSubmit(String query) {
   //action when press button search
   Log.d(TAG,"query string is:"+query);
   return true;
  }
  
  @Override
  public boolean onQueryTextChange(String newText) {
   return false;
  }
 };

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  mSwitch.setChecked(isChecked);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  Log.d(TAG,"onOptionItemSelected title is:"+item.getTitle());
  switch(item.getItemId()){
  case android.R.id.home:
   finish();
   break;
  case R.id.query:
   break;
  case R.id.search:
   break;
  case R.id.share:
   break;
   default:
    break;
  }
  return true;
 }
    
 public void onQuery(MenuItem item){
  Log.d(TAG,"select item is:"+item.getTitle());
 }
}

 

res/menu/activity_main.xml



    

    

    
          
            
            
        
    

 


关注我的技术公众号,查看更多优质技术文章推送

微信扫一扫下方二维码即可关注:

关注我的技术公众号,查看更多优质技术文章推送

 

你可能感兴趣的:(Android,ApiDemos)