如何使AlertDialog阻止code? [英] how to make AlertDialog block the code?

查看:138
本文介绍了如何使AlertDialog阻止code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以显示与OK按钮一个消息。我使用AlertDialog为此,我意识到这是不堵code。例如:

I am trying to show a messagebox with OK button. I am using AlertDialog for this purpose and I realised that it is not blocking the code. Example:

@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...
    }

当我开始活动,它显示警报2,当我preSS确定它显示报警1之后。

When I start activity, it shows Alert2, When I press ok it shows Alert1 afterwards.

我需要有阻塞code对话,所以首先它应该显示报警1消息,等到用户presses OK按钮,然后继续执行code和显示警报2消息等。例

I need to have blocking code dialog, so at first it should show Alert1 message, wait until user presses OK button then continue to execute the code and show Alert2 message, etc.. Example:

@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();
   */
}

我应该写上什么// ?????在MSGBOX方法放置?

What should I write on //????? place in msgBox method?

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

MORE EXAMPLE, I need something like this:

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

但是,这并不正常工作。活动结束的运行速度比快警报出现在屏幕上。

But this does not work. Activity finish runs quicker than alert appear on the screen.

有消息框code分离,以自己的方法,使其可重复使用的主要思路。如何实现这一目标?

The main idea to have messagebox code separate to own method to make it reusable. How to achieve this?

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

///////////////////////////////// another example:

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...
}

和作为替代这样的:

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...
}

你看到在复杂的区别?为了使初始化code Android中工作,我需要将其拆分为单独的方法,但不是在逻辑块,但是当我需要显示的对话框。此外,该code初始化对话框重复,成为丑陋,无法读取。

do you see the difference in complexity? In order to make init code working in Android I need to split it to separate methods but not on logical blocks but when I need to show dialogs. Moreover the code for initialization dialogs are repeated and became ugly and unreadable.

推荐答案

把code,以显示第一警报对话框的正面按钮的onClick第二个对话框。

Put the code to show 2nd dialog in onClick of Positive button of First alert dialog.

这篇关于如何使AlertDialog阻止code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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