【发布时间】:2015-03-08 09:53:56
【问题描述】:
编辑:代码现在可以工作了。我最终从 createDialog() 中调用了 loadingIllusionLoader()...
我试图在用户按下按钮后显示“假”进度条。我希望进度条出现一些随机时间〜 2000 毫秒,然后出现一个对话框以及隐藏进度条(因为它“加载”)。
我被告知尝试使用处理程序,因为 Thread.sleep 锁定了 UI,而我真的不想这样做。但是,一旦我执行下面的代码,它就会运行处理程序的 postDelayed 函数,并且每隔一段时间就会出现一个新的对话框......处理程序一遍又一遍地执行自己。如何停止处理程序。处理程序上的 removeCallbacksAndMessages 函数是一个选项,但我不确定如何完全停止打开对话框。
public void loadingIllusionLoader()
{
ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
theCircleLoader.setVisibility(View.VISIBLE);
int timeToRest = (int) (Math.random() * 1000) + 1500;
final Handler newHandle = new Handler();
newHandle.postDelayed(new Runnable() {
@Override
public void run() {
createDialog();
hidingIllusionLoader();
newHandle.removeCallbacksAndMessages(null);
}
}, timeToRest);
}
public void hidingIllusionLoader()
{
ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
theCircleLoader.setVisibility(View.INVISIBLE);
}
【问题讨论】: