我正在开发一个辅助功能应用程序。当用户想要离开应用程序时,我会显示一个对话框,他必须在其中确认他想离开,如果他在 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 秒后关闭对话框?
原文由 lisovaccaro 发布,翻译遵循 CC BY-SA 4.0 许可协议