在android中一次只显示一个进度对话框?

时间:2014-03-05 11:49:23

标签: android android-alertdialog progressdialog android-dialog

我想知道android支持多个对话框?从我第一次打开对话框开始。我允许用户点击取消,那将再打开一个对话框确认用户是否要退出,问题是如果用户不想退出,原来的对话框会消失,如何解决问题?感谢

public static ProgressDialog showProgressDialog(final Context ctx, boolean isCancelable) {
    ProgressDialog dialog =  new ProgressDialog(ctx);
    dialog.setCancelable(false);
    dialog.setTitle(ctx.getResources().getString(R.string.system_info));
    dialog.setMessage(ctx.getResources().getString(R.string.downloading));
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);

    if (isCancelable){
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, ctx.getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AlertDialog.Builder builder = new Builder(ctx);
                builder.setMessage(R.string.cancel_offline_mode);
                builder.setTitle(R.string.system_info);
                builder.setPositiveButton(ctx.getResources().getString(R.string.confirm), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton(ctx.getResources().getString(R.string.cancel), null);
                builder.create().show();
            }
        });
    }
    return dialog;
}

更新

这是我的期望:

  1. 显示下载进度的第一个对话框(例如,已下载60%等)。
  2. 在第一个对话框中有一个取消按钮,如果单击它则显示第二个对话框
  3. 第二个对话框询问是否确认取消下载
  4. 如果确认,请取消()...
  5. 如果没有,请返回第一个对话框并显示百分比
  6. 由于

3 个答案:

答案 0 :(得分:4)

这是我的功能,其中首先打开的对话框显示“拍照”,“添加视频”..当您点击“拍照”时,然后打开另一个显示“相机”或“画廊”的对开等等。希望它适合你。

private void popupPhotoSelector(){
      Utilities.alert_video("", null, 
     // Utilities.alert("", null,
        new AlertDialogActionSettings("Take Picture", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                overwritePhoto = true;
                takePicture();
            }
        }), 
        new AlertDialogActionSettings("Gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                overwritePhoto = true;
                openGallery();
            }
        }), 

         /////////////////////  Add Video //////////////////////////////
        new AlertDialogActionSettings("Add Video", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Intent intent = new Intent(getBaseContext(), Main_Vevue_Activity.class);
                //startActivity(intent);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);
                // set title
                alertDialogBuilder.setTitle("MLI");
                // set dialog message
                alertDialogBuilder
                        .setMessage("Take Video From!")
                        .setCancelable(true)
                        .setPositiveButton("Camera",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        overwritePhoto = true;
                                        takeVideo();
                                    }
                                })
                        .setNegativeButton("Gallery",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        overwritePhoto = true;
                                        openGalleryForVideo();  
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();


            }
        }), 
         /////////////////////  Add Video End //////////////////////////////
        null, false
    );
}

答案 1 :(得分:1)

通过关闭第二个对话框,您必须自己先调用对话框并根据需要发送任何结果数据。

答案 2 :(得分:1)

试试这个

public void showProgressDialog(final Context ctx, boolean isCancelable) {
        ProgressDialog dialog =  new ProgressDialog(ctx);
        dialog.setCancelable(false);
        dialog.setTitle("Title dialog");
        dialog.setMessage("downloading");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);

        if(isCancelable)
        {
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   showDialog2(ctx);
                }

            });
        }
        dialog.show();
    }

    private void showDialog2(final Context ctx) {
         AlertDialog.Builder builder = new Builder(ctx);
           builder.setMessage("cancel");
           builder.setTitle("dialog title");
           builder.setPositiveButton("cancel", new OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   showProgressDialog(ctx, false);
                   dialog.dismiss();
               }
           });
           builder.setNegativeButton("confirm", null);
           builder.create().show();                 
    }