Ответ 1
Просто просмотрел ProgressDialog.java
, нет официального пути.
Возможные варианты:
-
Подкласс, доступ
mProgressNumber
через отражение к.setVisibility(View.GONE);
-
Напишите свою собственную реализацию.
Я следил за примером диалога прогресса в ApiDemos. все прошло отлично, за исключением одного: я хочу удалить числа, которые появляются под панелью (те, которые работают с номерами от 0 до .getMax().
не удалось найти, как это сделать.
кто-нибудь?
Оп
Просто просмотрел ProgressDialog.java
, нет официального пути.
Возможные варианты:
Подкласс, доступ mProgressNumber
через отражение к .setVisibility(View.GONE);
Напишите свою собственную реализацию.
С api 11 мы можем сделать это, позвонив:
progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
Используйте этот код для 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);
}
}
}
Для полноты я использую ответ 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) {
}
}
}
С этим вы также можете скрыть процент.
Я принял @MartinVonMartinsgrün ответ и немного изменил его. Это решение просто использует API-вызов в Honeycomb +, тогда как он использует отражение для достижения той же цели для 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 not ideal but it works.
Log.w("ProgressDialog", "Failed to hide the progress number via reflection.", e);
}
}
};
}
// Further configure the dialog ...
dialog.show();