ProgressDialog 中的进度单元

发布于 2024-09-14 04:20:53 字数 430 浏览 8 评论 0原文

Android 的 ProgressDialog 允许您将当前进度和最大值设置为整数。这些值在对话框中显示如下:

3401/10023

其中第一个数字是当前进度,第二个数字是最大值。

我还想显示测量单位。像这样的事情:

3401/10023 知识库

使用 ProgressDialog 可以实现这一点吗?如果没有,您建议如何向用户提供此信息?我试图避免重新实现 ProgressDialog 只是为了包含该单元。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2024-09-21 04:20:53

更新:setProgressNumberFormat 自 11 级起成为 API 的一部分。

HEADProgressDialog 源代码已经包含一个名为的公共函数setProgressNumberFormat 可用于设置单位。不幸的是这个功能在最新的Android版本中似乎还没有提供。我想它将包含在未来的更新中。

同时,复制 ProgressDialog 的实现是最好的选择。子类化 ProgressDialog 是没有用的,因为它的所有成员都是私有的,并且使用 view.findViewById(R.id.progress_number) 来获取 TextView 直接极大有风险,因为没有什么可以确保 id 始终相同(或者 TextView 始终存在)。

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).

罪歌 2024-09-21 04:20:53

从API 11开始,您可以调用以下函数来达到您的目的。

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

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

mProgressDialog.setProgressNumberFormat("%1d/%2d kB") 
拥有 2024-09-21 04:20:53

在 ProgressDialog 源文件中:

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

您必须重新实现它,您无法避免它

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

绝影如岚 2024-09-21 04:20:53

对我来说,我可以设置并不明显:

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

也许这种最简单方式对其他人来说并不明显......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文