Android - 正在加载,请稍候

时间:2009-12-30 16:48:59

标签: android multithreading

当我调用一些AsyncTask(例如从远程服务下载一些数据)时,我可以在Android开发中使用标准的“正在加载,请稍候”对话框吗?

3 个答案:

答案 0 :(得分:74)

你的意思是不确定的ProgressDialog

编辑:即

ProgressDialog dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);

完成后调用dialog.dismiss()

答案 1 :(得分:19)

如果您实现runnable以及扩展Activity,那么您可以像这样处理代码......

private ProgressDialog pDialog;

public void downloadData() {
    pDialog = ProgressDialog.show(this, "Downloading Data..", "Please wait", true,false);
    Thread thread = new Thread(this);
    thread.start();
}

public void run() {
// add downloading code here
    handler.sendEmptyMessage(0);
 }

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        pDialog().dismiss();
        // handle the result here
    }
};

值得一提的是,您可以设置进度对话框的内容视图,以便显示自定义消息/图像:)

pDialog.setContentView(R.layout.X); 

答案 2 :(得分:10)

Mirko基本上是正确的,但有两点需要注意:

  1. ProgressDialog.show()是一种自动创建对话框的快捷方式。与其他对话框不同,它不应在onCreateDialog()中使用,因为它会导致Android 1.5出错。

  2. AsyncTask + ProgressDialog +屏幕方向更改还有一些问题需要注意 - check this out