ProgressDialog
我像这样创建一个onCreateDialog()
:
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_PROGRESS_ID)
{
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(getResources().getString(R.string.MyLabel));
dialog.setCancelable(false);
dialog.setIndeterminate(true);
return dialog;
}
}
Android 以它的智慧(或严重缺乏智慧)决定缓存通过 onCreateDialog() 创建的每个对话框。因此,任何后续调用都会showDialog(DIALOG_PROGRESS_ID)
导致使用相同的 ProgressDialog 实例,但动画已停止工作。
我试图重新设置 indeterminate in onPrepareDialog()
,但这没有任何作用。同样没有明显的方法可以调用将重置动画的对话框实例。
protected void onPrepareDialog(int id, Dialog dialog)
{
//This doesn't do anything
if (id == DIALOG_PROGRESS_ID)
((ProgressDialog)dialog).setIndeterminate(true);
super.onPrepareDialog(id, dialog);
}
编辑:但也许有办法让 ProgressBar 本身并开始动画?所以我在问了这个问题后尝试了以下方法:
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
if (id == DIALOG_PROGRESS_ID)
{
ProgressBar p = (ProgressBar) dialog.findViewById(android.R.id.progress);
if (p.getAnimation() != null)
p.startAnimation(p.getAnimation());
}
super.onPrepareDialog(id, dialog);
}
但它也没有工作!
那么,有谁知道是否有办法在 ProgressDialog 上重新启动动画?如果没有,有没有办法可以强制每个 showDialog() 调用调用 onCreateDialog()?(这第二个问题由@TuomasR 回答,但经过深思熟虑后我认为这不是一个很好的解决我的问题的方法)