gpt4 book ai didi

java - 抽屉导航上的 Back Pressed Exit 对话框 Back Pressed 不起作用

转载 作者:行者123 更新时间:2023-12-02 09:58:17 28 4
gpt4 key购买 nike

我想实现抽屉导航后按对话框上的对话框后按。该对话框有两个按钮,当我单击“确定”时,它会退出应用程序。问题是对话框没有显示...

这是我的抽屉导航 onBackpressed 代码....

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);

} else {
super.onBackPressed();
Toast.makeText(this, "BACK PRESSED", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Option");
builder.setMessage("Are You Sure To Exit??");
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
dialog.cancel();

}
});
builder.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(DialogInterface dialog, int which) {
//go to Remove Item
finishAffinity();
dialog.cancel();

}
});

最佳答案

试试这个代码

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);

} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Exit Application?");
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
})

.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

dialog.cancel();
}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}

关于java - 抽屉导航上的 Back Pressed Exit 对话框 Back Pressed 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55828583/

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