ProgressDialog - 如何删除数字

发布于 2024-09-01 01:09:21 字数 132 浏览 10 评论 0原文

我正在关注 ApiDemos 中的进度对话框示例。 一切都很顺利,除了一件事 - 我想删除出现在栏下方的数字(那些从 0 到 .getMax() 的运行数字。

找不到如何做到这一点。

有人吗?

Ori

I was following the progress dialog example in the ApiDemos.
all went great except for one thing - I want to remove the numbers that appear underneath the bar (those running numbers that run from 0 to .getMax().

couldn't find how to do it.

anyone?

Ori

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

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

发布评论

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

评论(5

江湖彼岸 2024-09-08 01:09:22

我采纳了@MartinVonMartinsgrün 的答案并对其进行了一些修改。该解决方案只是利用 Honeycomb+ 中的 API 调用,而它使用反射来实现 Gingerbread 和之前的相同目标。

ProgressDialog dialog;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    dialog = new ProgressDialog(getContext());
    dialog.setProgressNumberFormat(null);
} else {
    dialog = new ProgressDialog(getContext()) {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try {
                Field field = ProgressDialog.class.getDeclaredField("mProgressNumber");

                field.setAccessible(true);
                TextView textView = (TextView)field.get(this);
                field.setAccessible(false);

                textView.setVisibility(View.GONE);
            } catch (Exception e) {
                // Ignore the exception ... We'll just let the dialog show the bytes. It's not ideal but it works.
                Log.w("ProgressDialog", "Failed to hide the progress number via reflection.", e);
            }
        }
    };
}

// Further configure the dialog ...

dialog.show();

I took @MartinVonMartinsgrün's answer and modified it a little bit. This solution simply takes advantage of the API call in Honeycomb+, whereas it uses reflection to accomplish the same goal for Gingerbread and before.

ProgressDialog dialog;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    dialog = new ProgressDialog(getContext());
    dialog.setProgressNumberFormat(null);
} else {
    dialog = new ProgressDialog(getContext()) {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try {
                Field field = ProgressDialog.class.getDeclaredField("mProgressNumber");

                field.setAccessible(true);
                TextView textView = (TextView)field.get(this);
                field.setAccessible(false);

                textView.setVisibility(View.GONE);
            } catch (Exception e) {
                // Ignore the exception ... We'll just let the dialog show the bytes. It's not ideal but it works.
                Log.w("ProgressDialog", "Failed to hide the progress number via reflection.", e);
            }
        }
    };
}

// Further configure the dialog ...

dialog.show();
自由如风 2024-09-08 01:09:21

使用 api 11,我们可以通过调用:

progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);

With api 11, we can do it by calling:

progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
萌辣 2024-09-08 01:09:21

使用此代码适用于 Android < API 级别 11。它使用反射将可见性设置为 GONE:

public class CustomProgressDialog extends ProgressDialog {

public CustomProgressDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        Method method = TextView.class.getMethod("setVisibility",
                Integer.TYPE);

        Field[] fields = this.getClass().getSuperclass()
                .getDeclaredFields();

        for (Field field : fields) {
            if (field.getName().equalsIgnoreCase("mProgressNumber")) {
                field.setAccessible(true);
                TextView textView = (TextView) field.get(this);
                method.invoke(textView, View.GONE);
            }
        }
    } catch (Exception e) {
        Log.e(TAG,
                "Failed to invoke the progressDialog method 'setVisibility' and set 'mProgressNumber' to GONE.",
                e);
    }
}
}

Use this code for Android < API Level 11. It uses reflection to set the visbility to GONE:

public class CustomProgressDialog extends ProgressDialog {

public CustomProgressDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        Method method = TextView.class.getMethod("setVisibility",
                Integer.TYPE);

        Field[] fields = this.getClass().getSuperclass()
                .getDeclaredFields();

        for (Field field : fields) {
            if (field.getName().equalsIgnoreCase("mProgressNumber")) {
                field.setAccessible(true);
                TextView textView = (TextView) field.get(this);
                method.invoke(textView, View.GONE);
            }
        }
    } catch (Exception e) {
        Log.e(TAG,
                "Failed to invoke the progressDialog method 'setVisibility' and set 'mProgressNumber' to GONE.",
                e);
    }
}
}
樱桃奶球 2024-09-08 01:09:21

为了完整起见,我使用 Martin 的答案来构建我的课程:

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CustomProgressDialog extends ProgressDialog {

    private int progressPercentVisibility = View.VISIBLE;
    private int progressNumberVisibility = View.VISIBLE;

    public CustomProgressDialog(Context context, int progressPercentVisibility, int progressNumberVisibility) {
        super(context);

        this.progressPercentVisibility = progressPercentVisibility;
        this.progressNumberVisibility = progressNumberVisibility;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setFieldVisibility("mProgressPercent", progressPercentVisibility);
        setFieldVisibility("mProgressNumber", progressNumberVisibility);
    }

    private void setFieldVisibility(String fieldName, int visibility) {
        try {
            Method method = TextView.class.getMethod("setVisibility", Integer.TYPE);

            Field[] fields = this.getClass().getSuperclass()
                    .getDeclaredFields();

            for (Field field : fields) {
                if (field.getName().equalsIgnoreCase(fieldName)) {
                    field.setAccessible(true);
                    TextView textView = (TextView) field.get(this);
                    method.invoke(textView, visibility);
                }
            }
        } catch (Exception e) {
        }
    }
}

通过这个,您还可以隐藏百分比。

For completeness, I use the answer of Martin to build my class:

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CustomProgressDialog extends ProgressDialog {

    private int progressPercentVisibility = View.VISIBLE;
    private int progressNumberVisibility = View.VISIBLE;

    public CustomProgressDialog(Context context, int progressPercentVisibility, int progressNumberVisibility) {
        super(context);

        this.progressPercentVisibility = progressPercentVisibility;
        this.progressNumberVisibility = progressNumberVisibility;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setFieldVisibility("mProgressPercent", progressPercentVisibility);
        setFieldVisibility("mProgressNumber", progressNumberVisibility);
    }

    private void setFieldVisibility(String fieldName, int visibility) {
        try {
            Method method = TextView.class.getMethod("setVisibility", Integer.TYPE);

            Field[] fields = this.getClass().getSuperclass()
                    .getDeclaredFields();

            for (Field field : fields) {
                if (field.getName().equalsIgnoreCase(fieldName)) {
                    field.setAccessible(true);
                    TextView textView = (TextView) field.get(this);
                    method.invoke(textView, visibility);
                }
            }
        } catch (Exception e) {
        }
    }
}

With this you can also hide the percentage.

日裸衫吸 2024-09-08 01:09:21

刚查了一下ProgressDialog.java,没有官方的方法。

选择是:

  • 子类化它,通过反射到 .setVisibility(View.GONE) 访问 mProgressNumber

  • 编写您自己的实现。

Just looked through ProgressDialog.java, there's no official way.

The choices are:

  • Subclass it, access mProgressNumber via reflection to .setVisibility(View.GONE);

  • Write your own implementation.

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