如何在Android的ActionBar中为向上按钮添加侦听器? [英] How to add a listener for the up button in ActionBar on Android?
问题描述
我以这种方式在Android活动的操作栏中添加了向上按钮:
I added the up button in the action bar of my android activity in this way:
活动:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
清单:
android:parentActivityName =.MenuActivity">
android:parentActivityName=".MenuActivity">
工作正常,但现在我想在活动之间添加过渡效果. 此过渡效果很好:
It work fine but now i want to add a transition effect between activities. This transition work well:
Intent intent = new Intent(getApplicationContext(),MenuActivity.class); startActivity(intent); OverridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
Intent intent = new Intent(getApplicationContext(), MenuActivity.class); startActivity(intent); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
但是我应该在哪里放置此过渡代码?我的操作栏中的后退按钮没有监听器.
感谢咨询专家
推荐答案
操作栏中的向上按钮被视为ID为android.R.id.home
的菜单项,您可以在
The up button in the action bar is treated as a menu item with ID android.R.id.home
, as you can read in the docs. There you can find that you can handle clicks on it using this code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Respond to the action bar's Up/Home button
return false;
}
return super.onOptionsItemSelected(item);
}
这篇关于如何在Android的ActionBar中为向上按钮添加侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!