ApiDemos の進行状況ダイアログの例に従っていました。バーの下に表示される数字 (0 から .getMax() までの実行中の数字) を削除したいという 1 つのことを除いて、すべてうまくいきました。
それを行う方法が見つかりませんでした。
誰でも?
オリ
ApiDemos の進行状況ダイアログの例に従っていました。バーの下に表示される数字 (0 から .getMax() までの実行中の数字) を削除したいという 1 つのことを除いて、すべてうまくいきました。
それを行う方法が見つかりませんでした。
誰でも?
オリ
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) {
}
}
}
これにより、パーセンテージを非表示にすることもできます。
見ただけProgressDialog.java
で、公式の方法はありません。
選択肢は次のとおりです。
それをサブクラス化し、mProgressNumber
リフレクションを介してアクセスします.setVisibility(View.GONE);
独自の実装を作成します。
@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();