Андроид:диалоговое окно блокирующее выполнение кода.
От: kompotFX  
Дата: 04.10.11 09:15
Оценка:
Как сделать обычный диалог с кнопкой "ОК" чтобы пока не нажал, код за ним не выполнялся?

на других платформах это просто:

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


В андроиде пока я ничего подобного и простого не нашел. В качестве диалога есть AlertDialog. Но он не блокирует выполнение кода.
Например моя функция msgbox:
private void msgbox(String msg){
    new AlertDialog.Builder(activity)
        .setTitle("Test")
        .setMessage(msg)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

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

        })
        .setNegativeButton("", null)
        .show();
}


..естественно не работает как надо, код идет дальше и на экране юзер видит первым "more wrong stuff will be fixed" а потом "wrong stuff will be fixed", причем когда уже все пофикшено.


Ужасно кривое решение здесь конечно бы было что-то типа:

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


Но это по-моему просто ужас на костылях. Во-первых код диалога повторяется, во-вторых код инициализации разбивается на части не по логическому принципу а там где попался диалог. На примере еще сойдет, но будет совершенно нечитабельным в промышленном применении.

Как реализовать функцию msgbox чтобы она блокировала выполнение кода и не надо бы было переписывать инициализацию диалога кучу раз? Возможно ли такое на андроиде. (..и на яве вообще?)
android
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.