当我打开游戏并首先单击我的退出按钮时,它允许我结束游戏。但是当我打开游戏并单击我的开始按钮,然后返回到EXIT BUTTON所在的表单时,它会返回到上一个表单。我在这里尝试了一些答案,但没有任何反应。帮助我。
这是代码
Button button = (Button) findViewById(R.id.btnexit1);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Alert");
alertDialogBuilder.setMessage("Do you really want to exit?");
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setCancelable(false);
ad.setTitle("EXIT CONFIRMATION");
ad.setMessage("are you sure you want to exit?");
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
ad.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
ad.show();
}
});
}
}
答案 0 :(得分:0)
你应该覆盖你的OnBackPressed:
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed();
}