我已经实现了文件下载并将进度显示在对话框中。一切都工作得很好。现在,在实现我已经按照以下方式实现对话框代码的内容时:
@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下载对话框类似。
答案 0 :(得分: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) + "%)");
}
});
答案 1 :(得分:1)
我刚刚添加了
pDialog.setProgressNumberFormat(null);
到对话框,它的工作原理。