在Android中的警告框上设置倒计时计时器

3

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

2个回答

4
以下代码创建了一个像你描述的提示框。它会在默认按钮上添加倒计时计时器。 对话框监听类 请参考下图:screenshot of dialog with countdown
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();

2
创建一个带有 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()中关闭对话框。

如需更多详细信息,您可以访问此链接


@Saravanan,是的,为此使用自定义对话框。 - Akram
这是一个警告框对象,其中包含以下内容:object.setMessage("剩余秒数:" + millisUntilFinished / 1000); 请告诉我这是否正确。 - saravanan
我不认为这是正确的,我建议您使用自定义对话框。 - Akram
@saravanan 请点击以下链接查看自定义对话框的相关内容:http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog - Akram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接