Android - 加载中,请稍候

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:03:35  来源:igfitidea点击:

Android - Loading, please wait

androidmultithreading

提问by nikib3ro

Is there a standard "Loading, please wait" dialog I can use in Android development, when I invoke some AsyncTask (downloading some data from remote service for example)?

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

回答by Mirko N.

You mean something like an indeterminate ProgressDialog?

你的意思是像一个不确定的ProgressDialog

Edit: i.e.

编辑:即

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

then call dialog.dismiss()when done.

然后dialog.dismiss()完成后调用。

回答by Ally

If you implement runnable as well as extending Activity then you could handle the code like this...

如果您实现可运行以及扩展活动,那么您可以像这样处理代码......

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
    }
};

It's worth mentioning that you can set the content view of the progress dialog so you can display a custom message / image:)

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

pDialog.setContentView(R.layout.X); 

回答by Dan Lew

Mirko is basically correct, however there are two things to note:

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

  1. ProgressDialog.show()is a shortcut that automatically creates a dialog. Unlike other dialogs, it should NOT be used in onCreateDialog(), as it will cause errors in Android 1.5.

  2. There are some further issues with AsyncTask + ProgressDialog + screen orientation changes that you should be aware of - check this out.

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

  2. 您应该注意 AsyncTask + ProgressDialog + 屏幕方向更改还有一些其他问题 -检查一下