如何开始在机器人的活动之前显示进度对话框?对话框、机器人、进度

2023-09-11 12:45:53 作者:该用户穷得没有

如何显示进度对话框(例如,而活动加载某些数据)的Andr​​oid?

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();