【问题标题】:How to create Progress Dialog in Android Application?如何在 Android 应用程序中创建进度对话框?
【发布时间】:2013-07-05 07:51:15
【问题描述】:

我正在开发应用程序以从 Internet 接收一些数据,同时接收我想要显示“进度对话框”的数据。我在我的应用程序中使用了 "AsyncTask"

问题是如何使用它以及如何显示100%之类的百分比?

请给我建议并给我一些例子。 谢谢你,对不起我的英语。

【问题讨论】:

标签: android android-asynctask progressdialog android-progressbar


【解决方案1】:

要显示进度对话框,您可以使用以下代码

 ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setMessage("Your message..");
                dialog.show();

在你调用异步任务之前,即在new YourTask.execute().之前

并且在asynctask的onPostExecute函数中可以使用

 dialog.dismiss();

关闭对话框。

【讨论】:

  • 用于显示进度百分比。您可以查看此示例...link
【解决方案2】:

你可以使用休闲方式:

public void launchBarDialog(View view) {
    barProgressDialog = new ProgressDialog(MainActivity.this);

    barProgressDialog.setTitle("Downloading Image ...");
    barProgressDialog.setMessage("Download in progress ...");
    barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
    barProgressDialog.setProgress(0);
    barProgressDialog.setMax(20);//In this part you can set the  MAX value of data
    barProgressDialog.show();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                // Here you should write your time consuming task...
                while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {

                    Thread.sleep(2000);

                    updateBarHandler.post(new Runnable() {

                        public void run() {

                            barProgressDialog.incrementProgressBy(1);//At this, you can put how many data is downloading by a time
                                                                     //And with the porcentage it is in progress
                        }

                    });

                    if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {

                        barProgressDialog.dismiss();

                    }
                }
            } catch (Exception e) {
            }
        }
    }).start();
}

希望对大家有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多