2011-12-09 153 views
0

我有一个警告框,通过下面的代码创建:Android的警告对话框

private AlertDialog makeAndShowDialogBox(){ 

     myDialogBox = 

      new AlertDialog.Builder(this) 
      //set message, title, and icon 
      .setTitle("Terminator") 
      .setMessage("Are you sure that you want to quit?") 
      .setIcon(android.R.drawable.btn_star) 

      //set three option buttons 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
       //whatever should be done when answering "YES" goes here 


        functionShow(myDialogBox); 

       }    
      })//setPositiveButton 
     /* .setNeutralButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
       //whatever should be done when answering "NO" goes here 
       msg = "Cancel " + Integer.toString(whichButton); 
       txtMsg.setText(msg); 
       } 
       })*/ 


      .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
       //whatever should be done when answering "NO" goes here 
       msg = "NO " + Integer.toString(whichButton); 
       txtMsg.setText(msg); 
      } 
      })//setNegativeButton 

      .create(); 

      return myDialogBox; 
    } 

警告框,只要用户选择OK消失。但是我希望警报框在消失之前仍然等待特定的时间,因为我想更改警报框中的消息。我该怎么做?

functionShow有如下定义:

void functionShow(final AlertDialog dia) 
    { 

     dia.setMessage("Generating Unique ID ... Please Wait "); 



     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       dia.setMessage("Notyfying the user ... Please Wait");   
      } 
     }, 10000); 


     Handler handler1 = new Handler(); 
     handler1.postDelayed(new Runnable() { 
      public void run() 
      { 
       dia.setMessage("Your unique id is 0001. Kindly wait for the other person to respond"); 
      } 
     }, 15000); 

} 

但是程序只要我点击确定检查出来......没有显示其他消息。

+0

似乎很unintutive UI。如果有人点击关闭某些东西,而是改变对话框的文字,他们可能会感到困惑。对话框关闭后,您能否显示Toast? –

+0

@StealthRabbi:当用户点击确定并且告诉用户等待执行一些动作时,消息改变 – Chandeep

+0

去添加图像和文本你想要显示在你的警报对话框... http://stackoverflow.com/一个/ 10861216/1428123和编辑在你的Java代码像下面http://stackoverflow.com/a/10861174/1428123 –

回答

0

只是一些从我身边猜测:

在另一个线程与某种在它thread.sleep

或者在具有while循环的asynctask中,等待可以继续并关闭对话框的结果?

+0

我试过后推迟和处理程序太..但它不工作:| – Chandeep

1

使用这样的定制AlertDialog

MyAlertDialog.java

public class MyAlertDialog extends AlertDialog { 

    public boolean allowedToDismiss = false; 
    public void dismiss() { 
     if(allowedToDismiss) { 
      super.dismiss(); 
     } 
    } 
} 

在哪里你把你的对话框类中:

MyAlertDialog myDialogBox;  
/*Code...*/ 

public void onClick(DialogInterface dialog, int whichButton) { 
    myDialogBox.allowedToDismiss = true; 
    /*Code...*/ 
}