如何开始在机器人的活动之前显示进度对话框? [英] How to display progress dialog before starting an activity in Android?

查看:168
本文介绍了如何开始在机器人的活动之前显示进度对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示进度对话框(例如,而活动加载某些数据)的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();

这篇关于如何开始在机器人的活动之前显示进度对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆