带有CountDown TImer的Android对话框

时间:2014-09-03 09:19:27

标签: android alertdialog android-alertdialog countdowntimer

在我的应用中,我将推送通知显示为一个对话框,其中包含两个名为的按钮。我需要在对话框的标题中显示一个计时器(20秒)。

如果用户点击,则应该转到活动 如果用户单击,则会在计时器结束前取消对话框 倒计时结束时,对话框应该消失。我该如何实现呢?

这是我的警告对话框方法

public void showAlertDialog(final Context context, String title, String message,
                            Boolean status) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting Icon to Dialog
    //alertDialog.setIcon(R.drawable.fail);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();

        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();

            dialog.cancel();
        }
    });

    // Showing Alert Message
    //alertDialog.setIcon(R.drawable.counter);
    alertDialog.show();
}

1 个答案:

答案 0 :(得分:7)

我不建议将倒计时添加到对话框的标题中。如果您将其添加到按钮,则对于用户来说更明显的是倒计时结束时会发生什么。

这是一个AlertDialog的代码。

AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Notification Title")
        .setMessage("Do you really want to delete the file?")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO: Add positive button action code here
            }
        })
        .setNegativeButton(android.R.string.no, null)
        .create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    private static final int AUTO_DISMISS_MILLIS = 6000;
    @Override
    public void onShow(final DialogInterface dialog) {
        final Button defaultButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        final CharSequence negativeButtonText = defaultButton.getText();
        new CountDownTimer(AUTO_DISMISS_MILLIS, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                defaultButton.setText(String.format(
                        Locale.getDefault(), "%s (%d)",
                        negativeButtonText,
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1 //add one so it never displays zero
                ));
            }
            @Override
            public void onFinish() {
                if (((AlertDialog) dialog).isShowing()) {
                    dialog.dismiss();
                }
            }
        }.start();
    }
});
dialog.show();

dialog with countdown screenshot