gpt4 book ai didi

android - 单击箭头图标时如何在抽屉导航的 Android 中使用 onBackPressed?

转载 作者:行者123 更新时间:2023-11-30 01:19:36 25 4
gpt4 key购买 nike

我正在使用抽屉导航,当我点击工具栏的箭头时,onBackPressed 方法不会运行,但是当我点击底部面板的背面时,movil 正在运行。我不明白如何使用箭头按钮来做同样的事情。

公共(public)类 MainActivity 扩展 AppCompatActivity 实现 NavigationView.OnNavigationItemSelectedListener, BlankFragment.OnFragmentInteractionListener {

ActionBarDrawerToggle toggle;
DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setFocusableInTouchMode(true);

toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

drawer.setDrawerListener(toggle);

toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

getSupportFragmentManager().beginTransaction()
.replace(R.id.vista, new BlankFragment())
.addToBackStack(null)
.commit();

}


@Override
public void onBackPressed() {
super.onBackPressed();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {

} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

drawer.closeDrawer(GravityCompat.START);
return true;
}

@Override
public void onFragmentInteraction() {

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction()
.replace(R.id.vista, new BlankFragment2())
.addToBackStack(null)
.commit();

}

最佳答案

首先启用操作栏的后退箭头..

ActionBar actionBar = getSupportActionBar();
if(actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
}

第二次使用 android.R.id.home 访问 onOptionsItemSelected() 上的按钮

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}

关于android - 单击箭头图标时如何在抽屉导航的 Android 中使用 onBackPressed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com