启动画面后,大约需要 6 秒才能在 Main Activity 中加载 onCreate 内容。所以我想在加载时显示一个进度对话框,这就是我所做的:
import ...
private ProgressDialog mainProgress;
public void onCreate(Bundle davedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgress = new ProgressDialog (Main.this);
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMessage("Loading... please wait");
mProgress.setIndeterminate(false);
mProgress.setMax(100);
mProgress.setProgress(0);
mProgress.show();
---some code---
mProgress.setProgress(50);
---some code---
mProgress.setProgress(100);
mProgress.dismiss();
}
它不起作用...屏幕保持黑色 5-6 秒,然后加载主布局。我不知道我哪里做错了:*(
最佳答案
您需要在其他线程(而不是 UI 线程)中启动您繁忙的代码。
在您的 Activity 中创建 ProgressDialog 并在线程中关闭它
mProgress = new ProgressDialog (Main.this);
Runnable runnable = new Runnable() {
@Override
public void run() {
// code that needs 6 seconds for execution
// after finishing, close the progress bar
mProgress.dismiss();
}
};
new Thread(runnable).start();
关于android - 未显示主要 Activity 的 onCreate 中的进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905856/