如何使AlertDialog阻止代码?

kom*_*tFX 5 android

我试图用OK按钮显示一个消息框.我为此目的使用AlertDialog,我意识到它没有阻止代码.例:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        })
        .setNegativeButton("", null)
        .show();

       new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2")
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {}
       })
       .setNegativeButton("", null)
       .show();
        //...continue with UI initialization here...
    }
Run Code Online (Sandbox Code Playgroud)

当我开始活动时,它会显示Alert2,当我按下确定后,它会显示Alert1.

我需要有阻止代码对话框,所以首先它应该显示Alert1消息,等到用户按下OK按钮然后继续执行代码并显示Alert2消息等.示例:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      msgBox("Test dlg", "Alert 1");
      msgBox("Test dlg", "Alert 2");
      //...continue with UI initialization here...
    }


private void msgBox(String title, String msg){

   //?????

   /*  WRONG, NON-BLOCKING
   new AlertDialog.Builder(this).setTitle(title).setMessage(msg)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        })
        .setNegativeButton("", null)
        .show();
   */
}
Run Code Online (Sandbox Code Playgroud)

我该怎么写// ????? 放在msgBox方法?

更多例子,我需要这样的东西:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   if (isInitialisationDataFailed()){
      msgBox("Alert", "Cannot open activity, sorry");
      myActivity.this.finish();
      return;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.活动完成比屏幕上显示的警报更快.

将消息框代码与自己的方法分开以使其可重用的主要思路.怎么做到这一点?

///////////////////////////////// 另一个例子:

private void init(){
  //init code here...
  if (isSomethingWhrong()){
    msgbox("wrong stuff will be fixed");
    //do fix wrong stuff here...
  }
  if (isAnotherthingWrong()){
    msgbox("more wrong stuff will be fixed");
    //do fix more wrong stuff....
  }
  //continue init code here...
}

private void msgbox(String msg){
    //BLOCKING DIALOG REALISATION here...
}
Run Code Online (Sandbox Code Playgroud)

作为替代方案:

private void init(){
  //init code here...
  handleWrongStuff();
}
private void handleWrongStuff(){
 if (isSomethingWhrong()){
   new AlertDialog.Builder(activity)
        .setTitle("Test")
        .setMessage("wrong stuff will be fixed")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                 //do fix wrong stuff here...
                 handleMoreWrongStuff();       
            }

        })
        .setNegativeButton("", null)
        .show();
 }
  else{
     handleMoreWrongStuff();   
  }
}

private void handleMoreWrongStuff(){
 if (isAnotherthingWrong()){
   new AlertDialog.Builder(activity)
        .setTitle("Test")
        .setMessage("more wrong stuff will be fixed")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                 //do fix more wrong stuff here...    
                 continueInit();  
            }

        })
        .setNegativeButton("", null)
        .show();
 }
  else{
    continueInit();  
  }
}

private void continueInit(){
  //continue init code here...
}
Run Code Online (Sandbox Code Playgroud)

你看到复杂性的差异吗?为了使init代码在Android中工作,我需要将它拆分为单独的方法,但不要拆分逻辑块,但是当我需要显示对话框时.此外,重复初始化对话框的代码变得丑陋且难以理解.

anu*_*har 4

将显示第二个对话框的代码放在第一个警报对话框的积极按钮的 onClick 中。