gpt4 book ai didi

java - android中警报框上的倒数计时器

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:03 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - android中警报框上的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com