【问题标题】:Cancel ProgressDialog after running between dialogs在对话框之间运行后取消 ProgressDialog
【发布时间】:2016-03-31 07:28:24
【问题描述】:

我正在尝试通过创建一系列可以在 Android 中使用的对话框菜单来模拟 Android 中的 USSD 交互。现在,(感谢帮助我的用户on here!)我拥有它,以便在对话之间,有一个进度对话框显示“USSD 代码正在运行...”,每个对话之间运行 2 秒。不过,现在我的问题是我无法在进度对话框运行后取消它。现在,当我完成对话时,有几个进度对话实例仍处于打开状态并正在运行。我知道我需要在代码中的某个地方包含 progress.cancel() 命令,但是我尝试的每个地方似乎都阻止了进度对话框全部运行。

Runnable progressRunnable;

public void FirstScreen() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    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")) {
                        //Go to FirstTimeUse menu
                        progressRunnable = new Runnable() {

                            @Override
                            public void run() {
                                FirstTimeUse();
                            }
                        };
                        USSDprogressDialog(progressRunnable);
                    } else if (choice.equals("6")) {
                        //Something else
                    } else {
                        //Do nothing
                        dialog.dismiss();
                    }
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // End session
                    dialog.dismiss();
                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();
}

以及实际的进度对话框辅助方法:

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

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

我尝试在 USSDProgressDialog 方法的末尾以及运行 USSDProgressDialog 时包含在所有对话框中。我还能在哪里包含它,以便每次计时器用完后它都会取消?谢谢!

【问题讨论】:

  • 您要指示哪个计时器?
  • 这是 USSDProgressDialog 方法中的一个,该处理程序在最后将可运行对象延迟 2 秒。 (它不是真正的“计时器”,但它可以工作)

标签: android timer dialog progressdialog


【解决方案1】:
final ProgressDialog progress = new ProgressDialog(this);

您需要将进度对话框的声明放在USSDprogressDialog 函数之外,以便可以从活动的所有其他函数访问它。全局初始化进度对话。

现在从正面按钮的onClickListener 尝试使用此示例代码。

if (choice.equals("5")) {
    //Go to FirstTimeUse menu
    progressRunnable = new Runnable() {
        @Override
        public void run() {
            FirstTimeUse();
            // Now cancel the progress dialog here. 
            if(progress != null) progress.cancel();
        }
    };

    USSDprogressDialog(progressRunnable);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    相关资源
    最近更新 更多