2010-12-02 87 views
31

我正在开发测验,并且需要用户在继续之前回答所有问题。当用户没有回答所有的问题时,我会显示一个简单的alertdialog通知他或她。问题是无论我做什么我都无法让alertdialog关闭。为什么不dialog.cancel工作`这是代码:如何关闭Android alertdialog

AlertDialog.Builder ad = new AlertDialog.Builder(this); 
ad.setTitle("Unanswered Questions"); 
ad.setMessage("You have not answered all the questions."); 
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
    dialog.cancel(); 
} 
}); 
ad.show(); 
+1

将代码包装在代码标签中,以便问题可读。 – jacknad 2010-12-02 15:17:11

+1

尝试解雇()? – Vjy 2010-12-02 15:17:30

+0

应该工作。我可以建议你尝试调试每一步,看看是否调用了ad.show()。哦,还有onClick方法。 – achie 2010-12-02 15:24:14

回答

24

尝试使用

dialog.dismiss()

而是采用

dialog.cancel()

+1

嗨感谢您的回复。我已经尝试过dialog.dismiss(),而且这也不起作用。所以我想我必须尝试达到调试每一步的建议,因为看起来好像我的代码是正确的。 – Engmex 2010-12-03 13:42:46

+5

dismiss()不适用于AlertDialog,仅适用于常规对话框 – 2011-03-15 11:24:38

1

的我会尝试把一个

Log.e("SOMETAG", "dialog button was clicked"); 

在代码中的dialog.dismiss()行之前查看它是否实际到达该部分。

8

,就把这行中的OnCreate()

Context mcontext=this;  

,并在它们下面的代码

final AlertDialog.Builder alert = new AlertDialog.Builder(
           mcontext); 
         alert.setTitle(title); 
         alert.setMessage(description); 
         alert.setPositiveButton("Ok", 
           new DialogInterface.OnClickListener() { 

            @Override 
            public void onClick(DialogInterface dialog, 
              int which) { 
             dialog.cancel(); 
            } 
           }); 
         alert.show(); 

试试这个代码。它运行成功使用这个变量..

7

使用setNegative按钮,不需要正面按钮!我保证你会成功的X

+1

为什么downvote? – 2015-09-30 12:32:21

0
final AlertDialog.Builder alert = new AlertDialog.Builder(mcontext); 
alert.setTitle(title); 
alert.setMessage(description); 
alert.setPositiveButton("Ok", 
new DialogInterface.OnClickListener() { 
    @Override         
    public void onClick(DialogInterface dialog,int which) { 
     cancelDialog(); //Implement method for canceling dialog 
     } 
    }); 
alert.show(); 
void cancelDialog() 
{ 
    //Now you can either use 
    dialog.cancel(); 
    //or dialog.dismiss(); 
} 
68

AlertDialog.Builder本身不包含dismiss()cancel()方法。

这是一个方便的课程,可以帮助您创建一个Dialog,有没有权限访问这些方法。

下面是一个例子:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

AlertDialog alert = builder.create(); 

alert.show(); 

然后你可以呼吁警报(而不是制造商)

+0

如何在使用构建器构建时使用点击侦听器上的警报...请解释 – 2013-07-25 23:30:23

0

你可以简单地重新启动您的alertdialog出现在活动或其他活动取决于alert.cancel()方法你的判断。 如果你想重新启动活动 使用这个 finish(); startActivity(getIntent());

2
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
AlertDialog alert = builder.create(); 
alert.show(); 

上面的代码工作,但确保你alert一个全局变量,所以你可以在方法的onClick内到达它。

0
alertDialog.setPositiveButton("SAVE", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         /*Write your code here */ 
         dialog.dismiss(); 
        } 
       }); 
     alertDialog.setNegativeButton("CANCEL", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 

        } 
       }); 
5
AlertDialog.Builder ad = new AlertDialog.Builder(this); 
ad.setTitle("Unanswered Questions"); 
ad.setMessage("You have not answered all the questions."); 
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
    dialog.dismiss(); 
} 
}); 
ad.show(); 
1

使用对话,而不是AlertDialog

AlertDialog没有dismiss()但AlertDialog有像setPositiveButton()按钮的一些方法。

如果您想要自定义对话框,我建议使用对话框。

3

回复旧帖子,但希望有人可能会发现这有用。做到这一点,而不是

final AlertDialog builder = new AlertDialog.Builder(getActivity()).create(); 

然后,您可以继续做,

builder.dismiss(); 
0

我试过PowerAktar的解决方案,但AlertDialog和建造一直保持独立的部分。那么如何获得“真正的”AlertDialog?

我发现我的解决方案在展会-对话:你写

ad.show(); 

,以显示该对话框。在show的帮助下,它说:“使用提供给此构建器的参数创建一个AlertDialog,并使用Dialog.show()的对话框。”所以对话最终在这里创建。 show() - Command的结果是AlertDialog本身。所以,你可以使用这个结果:

AlertDialog adTrueDialog; 
adTrueDialog = ad.show(); 

有了这个adTrueDialog有可能取消()...

adTrueDialog.cancel() 

或执行按钮对话框中命令:

Button buttonPositive = adTrueDialog.getButton(Dialog.BUTTON_POSITIVE); 
buttonPositive.performClick(); 
-2

如果你已经使用了正面和负面的按钮(就像我在我的项目中做的那样),你可以使用Neutral Button来关闭对话框。

我还注意到,在Android版本> 5时,通过单击对话框窗口外的对话框关闭,但在较旧的版本中,这不会发生。

ad.setNeutralButton("CLOSE", new DialogInterface.OnClickListener(){ 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // close dialog 
    } 
});