3

如何在我的警报框中显示倒数计时器。我想通知用户会话将在 5 分钟后结束,并在 android 的警报弹出框中显示一个正在运行的计时器

4

2 回答 2

2

创建一个带有 a 的自定义对话框TextView

CountDownTimer并在这样的类的帮助下更新该代码。

new CountDownTimer(300000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

您可以在 中关闭您的对话框onFinish()

有关更多详细信息,您可以点击此链接

于 2012-05-28T16:43:07.557 回答
2

下面的代码会按照您的描述创建一个提示。它将倒计时计时器添加到默认操作按钮。

带有倒计时的对话框截图 对话监听类

private static class DialogTimeoutListener
        implements DialogInterface.OnShowListener, DialogInterface.OnDismissListener {
    private static final int AUTO_DISMISS_MILLIS = 5 * 60 * 1000;
    private CountDownTimer mCountDownTimer;

    @Override
    public void onShow(final DialogInterface dialog) {
        final Button defaultButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        final CharSequence positiveButtonText = defaultButton.getText();
        mCountDownTimer = new CountDownTimer(AUTO_DISMISS_MILLIS, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (millisUntilFinished > 60000) {
                    defaultButton.setText(String.format(
                            Locale.getDefault(), "%s (%d:%02d)",
                            positiveButtonText,
                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished % 60000)
                    ));
                } else {
                    defaultButton.setText(String.format(
                            Locale.getDefault(), "%s (%d)",
                            positiveButtonText,
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1 //add one so it never displays zero
                    ));
                }
            }

            @Override
            public void onFinish() {
                if (((AlertDialog) dialog).isShowing()) {
                    // TODO: call your logout method
                    dialog.dismiss();
                }
            }
        };
        mCountDownTimer.start();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mCountDownTimer.cancel();
    }

警报对话框

AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Session Timeout")
        .setMessage("Due to inactivity, you will soon be logged out.")
        .setPositiveButton("Extend Session", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO: call your log out method
            }
        })
        .setNegativeButton("Log Out Now", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO: call method to extend session
            }
        })
        .create();
DialogTimeoutListener listener = new DialogTimeoutListener();
dialog.setOnShowListener(listener);
dialog.setOnDismissListener(listener);
dialog.show();
于 2017-07-05T18:49:14.847 回答