android中警报框上的倒计时器

时间:2012-05-28 16:36:47

标签: java android alertdialog android-alertdialog countdowntimer

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

2 个答案:

答案 0 :(得分: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()

中关闭对话框

有关详细信息,请follow this link

答案 1 :(得分:2)

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

screenshot of dialog with countdown Dialog Listener Class

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();