gpt4 book ai didi

java - Navigation Drawer Activity 在 Drawers Item 中设置点击事件

转载 作者:行者123 更新时间:2023-11-29 18:25:31 25 4
gpt4 key购买 nike

您好,我正在尝试从抽屉中在我的导航 View 中放置一个事件 Item click 它工作正常,但问题是我抽屉中的其他项目停止工作,似乎在我的导航 View 上放置一个项目单击事件会影响我的导航 Controller

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundy_clock);
dBhelper = new DBhelper(this);

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

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();

if(id == R.id.nav_logout){
AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
conDialog.setTitle("Confirm");
conDialog.setMessage("Are you sure you want to log out?");
conDialog.setCancelable(false);

conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(BundyClock.this, MainActivity.class));
Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
finish();
}
});

conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog sdialog = conDialog.create();
sdialog.show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});

}

最佳答案

您必须使用 NavigationUI.onNavDestinationSelected 来处理此问题。检查以下内容:

boolean handled = NavigationUI.onNavDestinationSelected(menuItem, navController);
if (!handled) {
if(id == R.id.nav_logout){
AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
conDialog.setTitle("Confirm");
conDialog.setMessage("Are you sure you want to log out?");
conDialog.setCancelable(false);

conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(BundyClock.this, MainActivity.class));
Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
finish();
}
});

conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog sdialog = conDialog.create();
sdialog.show();
}
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return handled;

关于java - Navigation Drawer Activity 在 Drawers Item 中设置点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59222923/

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