首先得到actionBar
@override
public void onCreate(){
ActionBar actionBar = getActionBar();
}
初始化actionBar的图标和SearchVIew
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.actions,menu);
SearchView searchView = (SearchView) menu.findItem(R.id.ic_search).getActionView();
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(this,query,Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(this,newText,Toast.LENGTH_SHORT).show();;
return true;
}
处理actionbar的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.ic_search){
Toast.makeText(this, "now search", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
效果图
actionbar
SearchVIew
- 点击actionbar的搜索按钮,则呈现SearchVIew的效果