我正在开发一个测验,我需要用户在继续之前回答所有问题.当用户没有回答所有问题时,我会显示一个简单的警报,告知他或她.问题是无论我做什么我都无法关闭alertdialog.为什么dialog.cancel不工作?`这是代码:
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(); 
Pow*_*tar 77
它AlertDialog.Builder本身不包含dismiss()或cancel()方法.
这是一个方便的类来帮助你创建一个对话框,其中DOES访问这些方法.
这是一个例子:
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
AlertDialog alert = builder.create();
alert.show();
然后,您可以alert.cancel()在警报(而不是构建器)上调用该方法.
把这一行放在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();
试试这段代码..它运行成功..
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(); 
回复一个旧帖子,但希望有人可能会觉得这很有用。改为这样做
final AlertDialog builder = new AlertDialog.Builder(getActivity()).create();
然后你可以继续做,
builder.dismiss();
| 归档时间: | 
 | 
| 查看次数: | 99667 次 | 
| 最近记录: |