gpt4 book ai didi

java - 5秒后Android关闭对话框?

转载 作者:IT老高 更新时间:2023-10-28 21:06:55 28 4
gpt4 key购买 nike

我正在开发一个无障碍应用。当用户想要离开应用程序时,我会显示一个对话框,他必须在其中确认他想要离开,如果他在 5 秒后没有确认,对话框应该会自动关闭(因为用户可能不小心打开了它)。这类似于您更改屏幕分辨率时在 Windows 上发生的情况(会出现警告,如果您不确认,它会恢复到以前的配置)。

这就是我显示对话框的方式:

AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
dialog.create().show();

如何在显示 5 秒后关闭对话框?

最佳答案

final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
final AlertDialog alert = dialog.create();
alert.show();

// Hide after some seconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (alert.isShowing()) {
alert.dismiss();
}
}
};

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.removeCallbacks(runnable);
}
});

handler.postDelayed(runnable, 10000);

关于java - 5秒后Android关闭对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445745/

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