0

当他按下后退按钮时,我试图向用户实现一个退出对话框,但是当他按下它时,它会将他发送到先前的活动而不显示对话框。

这是onBackPressed():

        @Override
        public void onBackPressed() {
        new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Closing Activity")
            .setMessage("Are you sure you want to close this activity?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();    
            }

        })
        .setNegativeButton("No", null)
        .show();
    }
4

1 回答 1

0

只需添加.Create()并且只是添加super.finish()退出父 Activity 的建议。

public void onBackPressed() {
 new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Closing Activity")
        .setMessage("Are you sure you want to close this activity?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
       {
        @Override
        public void onClick(DialogInterface dialog, int which) {
           super.finish();    
        }

        })
      .setNegativeButton("No", null)
      .create().show();
}
于 2013-02-26T19:24:37.013 回答