onBackPressed显示alertDialog。单击退出按钮时,它显示主要活动。再次按下返回键时,将显示alertDialog,但是这次单击退出按钮将关闭应用程序。我想念什么?
这是OpenExitDialog()代码。
public void OpenExitDialog() {
try {
com.heyzap.sdk.ads.InterstitialAd.fetch();
if (com.heyzap.sdk.ads.InterstitialAd.isAvailable()) {
com.heyzap.sdk.ads.InterstitialAd.display(MainActivity.this);
} else {
start_AppAd.loadAd(new AdEventListener() {
@Override
public void onReceiveAd(Ad ad) {
start_AppAd.showAd(new AdDisplayListener() {
@Override
public void adHidden(Ad ad) { }
@Override
public void adDisplayed(Ad ad) { }
@Override
public void adClicked(Ad arg0) { }
@Override
public void adNotDisplayed(Ad arg0) { }
});
}
@Override
public void onFailedToReceiveAd(Ad ad) {
}
});
}
try {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Exit");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Thankyou!")
.setCancelable(false)
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
尝试一下
@Override
public void onBackPressed() {
AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Do you want to exit application?")
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
finish();
//close();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
希望它有用。
答案 1 :(得分:-1)
.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
System.exit(0); //add this line
}
});