Android AlertDialog dismiss方法不起作用

时间:2014-10-26 19:27:07

标签: java android dialog alertdialog

我已经创建了一个AlertDialog,但我无法关闭它... 我可以看到它并按下按钮,但是当我按下“肯定按钮”时,程序将执行一些任务然后关闭(关闭)对话框。所有任务都在完成,没有关闭对话框,它只会闪烁一次(它可能会关闭,然后重新打开,我不知道)。

AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
        builder.setTitle("Congratulations, you won!");
        builder.setMessage("Time: x seconds\nScore: xxxx\nHigh score: yyyy");
        builder.setPositiveButton("Play again!",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Doing stuff!
                        circle1Paint = yellowPaint;
                        circle2Paint = yellowPaint;
                        circle3Paint = yellowPaint;
                        playing = true;


                        // Trying to close it after doing "the stuff"
                        dialog.dismiss();
                    }
                });
        builder.setNegativeButton("Exit",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                    }
                });
        builder.create.show();

提前致谢! CoderOgden

2 个答案:

答案 0 :(得分:0)

进入退出按钮,输入dialog.dismiss()然后完成();

builder.setNegativeButton("Exit",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss(); 
                  finish();
                }
            });

答案 1 :(得分:0)

试试这段代码:

AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
    builder.setTitle("Congratulations, you won!");
    builder.setMessage("Time: x seconds\nScore: xxxx\nHigh score: yyyy");

    builder.setNegativeButton("Exit",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
    AlertDialog myDialog = builder.create();
    builder.setPositiveButton("Play again!",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Doing stuff!
                    circle1Paint = yellowPaint;
                    circle2Paint = yellowPaint;
                    circle3Paint = yellowPaint;
                    playing = true;


                    // Trying to close it after doing "the stuff"
                    myDialog.dismiss();
                }
            });
    myDialog.show();