【问题标题】:issue with dialog box on exit in androidandroid中退出对话框的问题
【发布时间】:2014-02-28 07:36:40
【问题描述】:

我开发了一个有一个活动的应用程序,我将在按下退出时编写代码, 我想在用户按下后退按钮时显示带有三个按钮的对话框如果显示那么它只是闪烁的对话框那么它背后的原因是什么

我的代码是

public class MainActivity extends Activity {

private AlertDialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

    alertDialogBuilder.setTitle("Alert Dialogue Box");

    alertDialogBuilder.setMessage("Are you sure?");

    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {

                    Toast.makeText(getApplicationContext(),
                            "Positive", 1000).show();

                }

            });

    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();

                    Toast.makeText(getApplicationContext(),
                            "You chose a negative answer",

                            Toast.LENGTH_LONG).show();

                }

            });

    alertDialogBuilder.setNeutralButton("Exit the app",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {

                    Toast.makeText(getApplicationContext(), "exit",
                            1000).show();
                    MainActivity.this.finish();

                }

            });

    AlertDialog alertDialog = alertDialogBuilder.create();

    alertDialog.show();

}

【问题讨论】:

  • 评论 super.onBackPressed();并再次检查。

标签: android android-alertdialog android-dialog


【解决方案1】:

你写的代码很好。但是这行导致Exception..

 super.onBackPressed();

这是完成你的Activity,所以你的Dialog 被窗口泄漏异常解除。 因此,请从您的代码中删除该行。

添加此行以取消对外部的触摸。

alertDialog.setCanceledOnTouchOutside(false);

【讨论】:

  • 当我在对话框外单击时,我还有一个关于此的问题,对话框关闭...
【解决方案2】:

您可以在对话框参考中设置 AlertDialog.Builder,例如:

Dialog dia = new AlertDialog.Builder(this)

并使用dia 来使用SetCanceledOnTouchOutside 方法

【讨论】:

    猜你喜欢
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多