见
http://developer.android.com/guide/topics/ui/actionbar.html#Handling
处理对操作项的点击
当用户按下某个动作时,系统将调用您的活动的onOptionsItemSelected()方法.使用传递给此方法的MenuItem,您可以通过调用getItemId()来识别该操作.这将返回标记的id属性提供的唯一ID,以便您可以执行相应的操作.例如:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
onUpButtonPressed();
return true;
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Note: If you inflate menu items from a fragment,via the Fragment
class’s onCreateOptionsMenu() callback,the system calls
onOptionsItemSelected() for that fragment when the user selects one of
those items. However,the activity gets a chance to handle the event
first,so the system first calls onOptionsItemSelected() on the
activity,before calling the same callback for the fragment. To ensure
that any fragments in the activity also have a chance to handle the
callback,always pass the call to the superclass as the default
behavior instead of returning false when you do not handle the item.
要将应用程序图标启用为向上按钮,请调用setDisplayHomeAsUpEnabled().例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}