【问题标题】:Hide the Progress of X/Y format while downloading and showing in Dialog在对话框中下载和显示时隐藏 X/Y 格式的进度
【发布时间】:2014-04-28 06:30:33
【问题描述】:

我已经实现了文件的下载并在对话框中显示进度。一切正常。现在在实现这些东西时,我以以下方式实现了对话框代码:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case progress_bar_type: // we set this to 0
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Fetching Attachment. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(false);
            pDialog.show();
            return pDialog;
        default:
            return null;
        }
    }

使用以下代码计算和显示百分比:

publishProgress((int) ((total * 100) / filelength));

我的 AsyncTask 方法:

protected void onPreExecute() {

            showDialog(progress_bar_type);
        }

        protected void onProgressUpdate(Integer... progress) {
            pDialog.setProgress(progress[0]);
        }

现在我的对话框正在显示下载进度,并且还以 %30/100 (即 X/Y 格式)显示进度更新。

现在我只想显示百分比而不是 X/Y 格式,类似于 android 中的 gmail 下载对话框。

【问题讨论】:

  • 添加的代码不显示任何 X/Y 或进度百分比。在此处添加正确的代码...

标签: android android-asynctask progressdialog


【解决方案1】:

试试看:

float downloadedSize = 0, totalSize = 0, per = 0, fdownloadedSize = 0,
        ftotalSize = 0;

NumberFormat numberFormat;

pDialog.setProgress((int) downloadedSize);
            numberFormat = new DecimalFormat("#.00");
            per = (downloadedSize / totalSize) * 100;
            fdownloadedSize = downloadedSize / (1024 * 1024);
            ftotalSize = totalSize / (1024 * 1024);
            // update the progressbar //
            runOnUiThread(new Runnable() {
                public void run() {
                    tvDownloadingPercent.setText("Downloaded "
                            + numberFormat.format(fdownloadedSize)
                            + "MB / " + numberFormat.format(ftotalSize)
                            + "MB (" + numberFormat.format(per) + "%)");


                }
            });

【讨论】:

    【解决方案2】:

    我刚刚添加了

    pDialog.setProgressNumberFormat(null);
    

    到对话框,它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多