Progress unit in ProgressDialog

99封情书 提交于 2020-01-10 10:35:40

问题


Android's ProgressDialog allows you to set the current progress and maximum value as integers. These values are shown in the dialog like this:

3401/10023

Where the first number is the current progress, and the second number is the maximum value.

I would like to also show the unit of measurement. Something like this:

3401/10023 KB

Is this possible using ProgressDialog? If not, what do you recommend doing to give this information to the user? I'm trying to avoid reimplementing ProgressDialog just to include the unit.


回答1:


Starting from API 11, you can call the following function to achieve your purpose.

mProgressDialog.setProgressNumberFormat("%1d/%2d kB") 



回答2:


Update: setProgressNumberFormat is part of the API since level 11.

The HEAD of the ProgressDialog source code already includes a public function called setProgressNumberFormat which can be used to set the unit. Unfortunately this function does not seem to be available in the latest Android version. I guess it will be included in a future update.

In the meantime, copying this implementation of ProgressDialog is the best option. Subclassing ProgressDialog is of no use because all of its members are private and working with view.findViewById(R.id.progress_number) to get the TextView directly is extremely risky, as nothing ensures that the id will be always the same (or that the TextView will always exist).




回答3:


In the ProgressDialog source file :

mViewUpdateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                /* Update the number and percent */
                int progress = mProgress.getProgress();
                int max = mProgress.getMax();
                double percent = (double) progress / (double) max;
                mProgressNumber.setText(progress + "/" + max);
                mProgressPercent.setText(mProgressPercentFormat.format(percent));
            }
        };

You must reimplement it, you cannot avoid it




回答4:


It wasn't obvious for me that I can just set:

mProgressDialog.setMessage("Downloading...(size in kB)");

Maybe this simplest way isn't obvious for somebody else...



来源:https://stackoverflow.com/questions/3492001/progress-unit-in-progressdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!