【发布时间】:2011-01-25 22:23:20
【问题描述】:
我想在android中设置Dialog(进度对话框)的超时时间,使对话框在一段时间后消失(如果某些操作没有响应!)
【问题讨论】:
我想在android中设置Dialog(进度对话框)的超时时间,使对话框在一段时间后消失(如果某些操作没有响应!)
【问题讨论】:
验证与this post 中的方法相同(使用 long 而不是 float):
public void timerDelayRemoveDialog(long time, final Dialog d){
new Handler().postDelayed(new Runnable() {
public void run() {
d.dismiss();
}
}, time);
}
【讨论】:
您始终可以创建一个名为 ProgressDialogWithTimeout 的类,并覆盖 show 方法的功能以返回 ProgressDialog 并设置一个计时器,以便在该计时器关闭时执行您希望的操作。示例:
private static Timer mTimer = new Timer();
private static ProgressDialog dialog;
public ProgressDialogWithTimeout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ProgressDialogWithTimeout(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
public static ProgressDialog show (Context context, CharSequence title, CharSequence message)
{
MyTask task = new MyTask();
// Run task after 10 seconds
mTimer.schedule(task, 0, 10000);
dialog = ProgressDialog.show(context, title, message);
return dialog;
}
static class MyTask extends TimerTask {
public void run() {
// Do what you wish here with the dialog
if (dialog != null)
{
dialog.cancel();
}
}
}
然后你会在你的代码中这样调用它:
ProgressDialog progressDialog = ProgressDialogWithTimeout.show(this, "", "Loading...");
【讨论】: