0

现在在我的点击事件中,我注意到有时在我的调试器上同时出现多个对话框时,它会稍微滞后 onClick。

我们将如何修复它,所以有办法让它只显示 1 AlertDialog?

代码:相当标准。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                Intent i = new Intent(class1.this, class2.class);
startActivity(i);
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create()
4

4 回答 4

1

使用isShowing()方法可以检查警报对话框是否显示..

和 onClick 事件每次单击时都会创建新对话框,因此请检查对话框是否不为空,如果不为空则创建对话框,然后检查.isShowing()

所以,

AlertDialog alert=null;//declare as a global ..mind that not in your onClick method

if(null=alert){ 
alert = new AlertDialog.Builder(this).create();
}

if(!alert.isShowing()){
   //do stuff here is dialog is showing here...
 } 
于 2012-06-05T13:55:08.837 回答
0
     public void onClick(View v) {                  
         send();
         alertdialog();
         alertdialog1();
        }

   //first
      void alertdialog(){    

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setMessage("Go to the next screen?")
          .setCancelable(false)
          .setPositiveButton("ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
              }
          });
          /*.setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          });*/
   AlertDialog alert = builder.create();
   alert.show();
   }  
    //////sexcond
      void alertdialog1(){    

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setMessage("Go to the next screen?")
          .setCancelable(false)
          .setPositiveButton("ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
              }
          });
          /*.setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          });*/
   AlertDialog alert = builder.create();
   alert.show();
   }  
于 2012-06-05T13:56:30.830 回答
0

嗯,试试这样:

private AlertDialog mDialog;

public void fillDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Go to the next screen?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            Intent i = new Intent(class1.this, class2.class);
            startActivity(i);
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
    mDialog = builder.create()
}

public void showDialog(){
     if (mDialog == null) createDialog();
     if (mDialog.isShowing()) return;
     mDialog.show();
}
于 2012-06-05T13:57:38.213 回答
0

如果您快速单击该按钮。它会将所有单击排队并一一处理。但你想要的是在第一次点击后忽略其余的点击。

boolean isClickable = true;

        btn.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                if (isClickable)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Go to the next screen?")
                            .setCancelable(false)
                            .setPositiveButton("Yes",
                                    new DialogInterface.OnClickListener()
                                    {
                                        public void onClick(
                                                DialogInterface dialog, int id)
                                        {
                                            Intent i = new Intent(class1.this,
                                                    class2.class);
                                            startActivity(i);
                                        }
                                    })
                            .setNegativeButton("No",
                                    new DialogInterface.OnClickListener()
                                    {
                                        public void onClick(
                                                DialogInterface dialog, int id)
                                        {
                                            dialog.cancel();
                                        }
                                    });
                    AlertDialog alert = builder.create();

                }
                isClickable = false;
            }
        });    
于 2012-06-05T13:58:59.313 回答