我在ApiDemos中按照进度对话框的示例进行操作。
一切都很顺利,除了一个问题- 我想要移除那些出现在进度条下方的数字(那些从0到.getMax()不断运行的数字)。
我找不到如何做到这一点。
有人知道怎么做吗?
Ori
我在ApiDemos中按照进度对话框的示例进行操作。
一切都很顺利,除了一个问题- 我想要移除那些出现在进度条下方的数字(那些从0到.getMax()不断运行的数字)。
我找不到如何做到这一点。
有人知道怎么做吗?
Ori
使用API 11,我们可以通过调用以下方法来实现:
progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
如果您的Android版本低于API Level 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);
}
}
}
为了完整起见,我使用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) {
}
}
}
通过这个,你也可以隐藏百分比。
我刚刚查看了ProgressDialog.java
文件,没有官方的方法。
可以选择以下两种方式:
继承该类,通过反射访问mProgressNumber
并将其设为.setVisibility(View.GONE);
编写自己的实现。
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();