AlertDialog android 工作室

AlertDialog android studio

我正在尝试显示一个包含加载动画的警告对话框。单击最初显示的另一个警报对话框后,需要出现警报对话框。基本上,第一个对话框包含一个 setPositive 和 setNegative 按钮。 setPositive 事件会触发一个需要几秒钟的过程。我希望包含加载动画的第二个对话框在进程开始时出现。在显示加载对话框的过程完成之前,我无法关闭第一个对话框。

//dialog that contains loading animation
    public class LoadingBox {
    Activity myActivity;
    AlertDialog dialog;

    LoadingBox(Activity activity){
        myActivity = activity;
    }
     void startLoadingDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
        LayoutInflater inflater = myActivity.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.layout_dialog,null));
        builder.setCancelable(true);
        dialog = builder.create();
        dialog.show();
    }
    void dismissDialog(){
        dialog.dismiss();
    }
}

 

      
     new AlertDialog.Builder(SmsListActivity.this)
    
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Add To Transaction List")
                            .setMessage("Please note that this process might take long. Proceed?")
                            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    LoadingBox loadingBox = new LoadingBox(SmsListActivity.this);
    
                                    loadingBox.startLoadingDialog();
    
                                    addPendingToList();
                                    loadingBox.dismissDialog();
    
    
                                }
                            }).setNegativeButton("No",null).show();

要关闭此警告对话框,您应该在 setPositiveButton onClick

中调用 dialog.dismiss();
 new AlertDialog.Builder(SmsListActivity.this) .setIcon(android.R.drawable.ic_dialog_alert)
           .setTitle("Add To Transaction List")
           .setMessage("Please note that this process might take long. Proceed?")
           .setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {                    
        dialog.dismiss();
        LoadingBox loadingBox = new LoadingBox(SmsListActivity.this);
    loadingBox.startLoadingDialog();
    addPendingToList();
    loadingBox.dismissDialog();
    }
      }).setNegativeButton("No",null).show();

如@MathankumarK 所说,调用 .dismiss();在你积极的 onClick 功能中也许是最好的选择,但你可以在 dismiss() 之后使用 Thread.sleep(----);等待几毫秒,直到操作开始,然后 显示“正在加载”对话框。由于该操作无论如何都会减慢应用程序线程的速度,因此减慢或停止您的应用程序几秒钟应该不会造成问题。