警报对话框驳回后甚至弹出 [英] Alert dialog pops up even after dismissing it

查看:138
本文介绍了警报对话框驳回后甚至弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的典型问题,它似乎很奇怪我。详细情况是这样的 - 在活动中我的应用程序中,有edittexts和提交按钮。填补了edittexts数据后,用户可以点击提交按钮。点击提交按钮,基于由用户输入的值之后,或在两个警报对话框被示出。一个是成功和另一种是失败。

当用户输入无效数据,并点击提交按钮,失败的警告对话框打开得到的东西是。我对失败的警告对话框按钮(OK),点击后,我写的 dialog.dismiss(); 以使其消失,所以该用户可以重新检查数据,并且可以修改。但问题是,同时复查和放大器;修改数据,如果他改变了方向,然后再次失败的警告对话框弹出,即使没有点击提交按钮。请建议。

额外的细节(虽然可能没有必要为这个问题):在改变方向的活动重新创建。所以,我现在的储蓄在onSavedInstanceState(当前数据)和检索的onCreate它()方法来设置回值在edittexts。一切工作正常,但一旦点击提交按钮,会出现相应的警告对话框。然后改变方向后的对话框再次弹出。我相信,我写的的ShowDialog(1); 的onClick()方式,但话又说回来,为什么控制会回的onClick并显示出警告对话框,即使没有点击。

 保护对话框onCreateDialog(INT ID){
   开关(ID){
      情况下0:
        返回新AlertDialog.Builder(本)
                      .setMessage(成功!)
                      .setIcon(R.drawable.success)
                      .setPositiveButton(OK,
                      新DialogInterface.OnClickListener(){
                         @覆盖
                         公共无效的onClick(DialogInterface对话,诠释它){
                            dialog.dismiss();
                         }
                      })。显示();
       情况1:
           返回新AlertDialog.Builder(本)
                      .setMessage(失败)
                      .setIcon(R.drawable.failure)
                      .setPositiveButton(OK,
                       新DialogInterface.OnClickListener(){                           @覆盖
                           公共无效的onClick(DialogInterface对话,诠释它){
                              dialog.dismiss();
                              返回;
                           }
                        })。显示();
           }
           返回null;
       }

下面是使警告对话框中显示的方式。

 公共无效的onClick(视图v){
   开关(v.getId()){
      //这里还有其他的情况了。
      案例R.id.submit:
        getEditTexts();
        验证器();
      打破;
   }
}公共无效验证器(){
    如果(generator.receiveVal(0,0,须藤)){
       的ShowDialog(0);
     }
    否则,如果(!generator.receiveVal(0,0,须藤)){
       的ShowDialog(1);
    }
}


解决方案

刚刚尝试在.show的地方更换.create()()。你的情况是这样的:

 案例1:
               返回新AlertDialog.Builder(本)               .setMessage(失败)
               .setIcon(R.drawable.failure)
               .setPositiveButton(OK,
                       新DialogInterface.OnClickListener(){                        @覆盖
                        公共无效的onClick(DialogInterface对话,诠释它){                            dialog.dismiss();                          返回;
                        }
                    })。创建(); //这里换成.show与.create()

I have come across a typical problem and it seems strange to me. Details are something like this - On the activity in my app, there are edittexts and submit button. After filling the data in the edittexts, user can click submit button. After clicking submit button, based on the values that are entered by the user, either of the two alert dialogs are shown. One is success and the other one is failed.

The thing is when the user enters invalid data and clicks submit button, the failed alert dialog gets opened. I have a button(OK) on the failed alert dialog, after clicking it I wrote dialog.dismiss(); to make it disappear, so that user can recheck the data and can modify. But the problem is while rechecking & modifying the data if he changes the orientation, then again the failed alert dialog is popping up even without clicking submit button. Please suggest.

Extra Details(though probably not necessary for this problem): While changing orientation the activity is recreated. So, I am saving the current data in the onSavedInstanceState() and retrieving it in onCreate() method to set back the values in the edittexts. Everything works fine, but once clicking on submit button, the respective alert dialog appears. Then after changing orientation the dialog is again popping up. I am sure that I wrote showDialog(1); in the onClick() method but then again why control is going back into onClick and showing that alert dialog even without clicking.

protected Dialog onCreateDialog(int id) {
   switch(id){                 
      case 0:          
        return new AlertDialog.Builder(this)
                      .setMessage("Success!") 
                      .setIcon(R.drawable.success)
                      .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();                   
                         }
                      }).show();
       case 1:
           return new AlertDialog.Builder(this)
                      .setMessage("Failed")
                      .setIcon(R.drawable.failure)
                      .setPositiveButton("OK",
                       new DialogInterface.OnClickListener() {

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

Here is the method that makes alert dialog show.

public void onClick(View v) {
   switch (v.getId()) {    
      //Here there are other cases too.
      case R.id.submit:
        getEditTexts();
        validator();
      break;
   }
}

public void validator() {              
    if(generator.receiveVal(0,0,sudo)) {
       showDialog(0);
     }
    else if(!generator.receiveVal(0,0,sudo)) {
       showDialog(1);
    }
}

解决方案

Just try replacing .create() in the place of .show(). In your case like this:

case 1:
               return new AlertDialog.Builder(this)

               .setMessage("Failed")
               .setIcon(R.drawable.failure)
               .setPositiveButton("OK",
                       new DialogInterface.OnClickListener() {

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

                            dialog.dismiss();

                          return;
                        }
                    }).create();   //Here replaced .show with .create()

这篇关于警报对话框驳回后甚至弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆