0

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

4

3 回答 3

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
    }
})
...
于 2012-06-05T07:35:09.057 回答
1

您可以setOnCancelListener为您的对话框设置一个侦听器并简单地调用finish()

于 2012-06-05T07:36:28.903 回答
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();
}

这应该可以解决您的问题:)

于 2012-06-05T07:34:37.530 回答