I was following the progress dia开发者_运维知识库log 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
With api 11, we can do it by calling:
progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
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);
}
}
}
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.
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.
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();
精彩评论