android:退出App(Activity)对话框关闭

时间:2012-06-05 07:30:35

标签: android dialog

我有一个活动打开一个对话框进行注册或登录。我想要的是,如果我按下后退按钮,不仅对话框而且活动应该退出。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

在对话框

上按下后退键时,使用setOnCancelListener来呼叫finish()
//for example
new AlertDialog.Builder(this)
.setOnCancelListener(new OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        finish();   //to finish Activity on which dialog is displayed
    }
})
...

答案 1 :(得分:1)

您可以为对话框设置setOnCancelListener听众,只需致电finish()

即可

答案 2 :(得分:0)

public void onBackPressed() {

       final Builder builder = new Builder(this);
        builder.setTitle(R.string.caption);
        builder.setMessage("Do you really want to exit?");
        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int which) {
                     YourActivity.this.finish();
                     dialog.dismiss();    
            }
        });
        builder.setNegativeButton(android.R.string.cancel,
                new OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog,
                            final int which) {
                                                  dialog.dismiss();
                    }
                });
        final AlertDialog dialog = builder.create();
        dialog.show();
}

这可以解决你的问题:)