English 中文(简体)
如何使警惕性组织阻止该守则?
原标题:how to make AlertDialog block the code?
  • 时间:2011-10-04 07:34:00
  •  标签:
  • android

我正试图向Kutton展示一个信息箱。 为此,我使用了警示组织,我认识到,它并不阻碍守则的实施。 例:

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

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

我需要阻止密码的辩词,因此首先应当显示警示1的信息,直到用户继续实施《科索沃报》和显示警2信息等。 例:

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

我应该写什么? 页: 1 方框方法?

我需要这样的东西:

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

但这并不可行。 活动结束比屏幕上显示的警示更快。

其主要想法是,信息箱代码要单独使用,以掌握可加以利用的方法。 如何做到这一点?

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

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.

问题回答

规定在第一警示区积极县的Click上显示第二位方言。

如果届时有人会援引顿语,你可以执行该守则,例如,当你向ok子施压时,那么你就可以停止处决,而不用这样做。

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

道歉并不阻碍执行守则——简便。 实现“类似锁定”行为的唯一办法是将执行链分成较小的轨道。

让我们假设,在倡议启动期间,总共有5个迪亚洛。 在你的活动中,你制定了以下方法:

private void beforeFirstDialog() {
   // Do the initialization until you want the first dialog
   // Show the first dialog
   // When clicking  OK  in the first dialog, beforeSecondDialog() is called.
}

private void beforeSecondDialog() {
   // Do the initialization until you want the second dialog
   // Show the second dialog
   // When clicking  OK  in the second dialog, beforeThirdDialog() is called.
}

private void beforeThirdDialog() {
   // Do the initialization until you want the third dialog
   // Show the third dialog
   // When clicking  OK  in the third dialog, beforeFourthDialog() is called.
}

private void beforeFourthDialog() {
   // Do the initialization until you want the fourth dialog
   // Show the fourth dialog
   // When clicking  OK  in the first dialog, beforeFifthDialog() is called.
}

private void beforeFifthDialog() {
   // Do the initialization until you want the fifth dialog
   // Show the fifth dialog
   // When clicking  OK  in the first dialog, afterFifthDialog() is called.
}

private void afterFifthDialog() {
   // Do what needs to be done after the last dialog.
}

这是很晚的答复,但可以结束。

The cleanest way to do what you want is to launch an activity instead of the dialog. A simple activity with just one OK button.

When the OK button is pressed just close the activity.
If you need more complex responses, more buttons, text input, etc.
use StartActivityWithResult() to return the user s choice

根据《宪法》第22条。

显示Alert.displayAlert(现有类别名称,即“请输入错误信息”);

          strDomain.requestFocus();

创立了新班级的展品。

公共课程表

    public static void displayAlert(Activity act,String msg)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(act);
        alert.setMessage(msg).setPositiveButton("OK", new DialogInterface.OnClickListener(){
             @Override
             public void onClick(DialogInterface dialog, int which)
             {   

             }
             })
             .show();
    }

}

EditText strDomain;

strDomain is the edittext name. it like when that field is null i have made it like unless it is filled it will not go to the next page.. what is your case when you want to show the message at want instance?

As you (might have) discovered from the many replies that you should not block the Main UI Thread. However, if you have a very good reason to block the code while you get a response from the user, then you could create a thread where you loop/wait for the user input while you the AlertDialog is displayed via the MainUI Thread. I have a full blog post here that explains this

protected void RunConfirmAction(Action runnableAction)
{
    var confirmThread = new Thread(() => runnableAction());
    confirmThread.Start();
}

// OnClick (when the user clicks Logout Button, I call this
RunConfirmAction(Logout);

// the implemtation of the MessageBox waiting looks like this:
public DialogResult MessageBoxShow(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
    if (_CurrentContext != null && MainActivity != null)
    {
        Action<bool> callback = OnConfirmCallBack;
        _IsCurrentlyInConfirmProcess = true;
        Action messageBoxDelegate = () => MessageBox.Show(((Activity)MainActivity), callback, message, caption, buttons);
        RunOnMainUiThread(messageBoxDelegate);
        while (_IsCurrentlyInConfirmProcess)
        {
            Thread.Sleep(1000);
        }               
    }
    return _ConfirmBoxResult ? DialogResult.OK : DialogResult.No;
}

private void OnConfirmCallBack(bool confirmResult)
{
    _ConfirmBoxResult = confirmResult;
    _IsCurrentlyInConfirmProcess = false;
}

private bool _ConfirmBoxResult = false;
private bool _IsCurrentlyInConfirmProcess = false;

I hope this can help somebody, the full details of the solution can be found here

Kotlin使生活容易,在联合路线内呼吁

suspendCoroutine<Context> { cont ->
     runOnUiThread {
            val builder = androidx.appcompat.app.AlertDialog.Builder(this@YourScreen)
            builder.setTitle("Title")
            builder.setMessage("I have a message for you")
            builder.setPositiveButton("Yes") { _, _ ->
                   /* Do something on yes */
            }
            builder.setNegativeButton("No") { _, _ ->
                   /* Do something on no */
            }
            builder.setOnCancelListener {
                  cont.resume(this@YourScreen)
            }
            val askdialog = builder.create()
            askdialog.show()
     }
}
//Code will continue here




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签