Show ProgressDialog between Dialogs

北战南征 提交于 2020-01-06 02:51:29

问题


I'm trying to mock up a USSD interaction in Android by creating a series of dialog menus that you can go through in Android. I'm trying to make it so that in between dialogs, there is a progress dialog that says "USSD code running..." However, upon clicking the positive button, when I try to run a ProgressDialog with a runnable timer and follow it with the next dialog called FirstTimeUser, they just layer one on top of another, even if I try to separate them with another timer. How can I make them run consecutively instead of simultaneously? Code snippets below:

    USSDprogressDialog();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View dialogView = inflater.inflate(R.layout.number_response_dialog, null);
    builder.setView(dialogView)
            .setMessage(R.string.MainMenuText)
            // Add action buttons
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // Send number to next dialog
                    String choice = getMenuChoice(dialogView);
                    if (choice.equals("5")) {
                        USSDprogressDialog();
                        FirstTimeUse();
                    } else {
                        //Do nothing
                    }
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // End session
                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();

And the progress dialog with a timer is:

public void USSDprogressDialog() {
    final ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("USSD code running...");
    progress.show();


    Runnable progressRunnable = new Runnable() {

        @Override
        public void run() {
            progress.cancel();
        }
    };

    Handler handler = new Handler();
    handler.postDelayed(progressRunnable, 2000);
}

Any suggestions would be welcome! Thank you!


回答1:


move FirstTimeUse() to the progressCancel of the dialog. maybe you need to make USSDprogressDialog(Runnable runnable)



来源:https://stackoverflow.com/questions/36231859/show-progressdialog-between-dialogs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!