Android 标题栏上加一个返回按钮

在Java页面onCreate方法中添加
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
重写onOptionsItemSelected方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish(); // back button
return true;
}
return super.onOptionsItemSelected(item);
}
本文介绍在Android应用中如何在标题栏上添加返回按钮,并通过Java代码实现返回功能。具体步骤包括在onCreate方法中启用ActionBar的返回按钮,以及重写onOptionsItemSelected方法来处理返回操作。
586

被折叠的 条评论
为什么被折叠?



