本文介绍了如何在 Android 中开始活动之前显示进度对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Android 中,您如何在启动 Activity 之前(即,当 Activity 加载一些数据时)显示进度对话框?
How do you display a progress dialog before starting an activity (i.e., while the activity is loading some data) in Android?
推荐答案
您应该在 AsyncTask 中加载数据 并在数据加载完成后更新您的界面.
You should load data in an AsyncTask and update your interface when the data finishes loading.
您甚至可以在 AsyncTask 的 onPostExecute()
方法中启动一个新活动.
You could even start a new activity in your AsyncTask's onPostExecute()
method.
更具体地说,您将需要一个扩展 AsyncTask 的新类:
More specifically, you will need a new class that extends AsyncTask:
public class MyTask extends AsyncTask<Void, Void, Void> {
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public void doInBackground(Void... unused) {
... do your loading here ...
}
public void onPostExecute(Void unused) {
progress.dismiss();
}
}
然后在你的活动中你会做:
Then in your activity you would do:
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();
这篇关于如何在 Android 中开始活动之前显示进度对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!