Переглянути джерело

Added progressNumberFormat() and progressPercentFormat() to the Builder, along with methods in MaterialDialog for after construction. Resolves https://github.com/afollestad/material-dialogs/issues/578

Aidan Follestad 10 роки тому
батько
коміт
33654251ac

+ 3 - 2
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -365,14 +365,15 @@ class DialogInit {
                 dialog.setTypeface(dialog.mProgressMinMax, builder.regularFont);
                 if (builder.showMinMax) {
                     dialog.mProgressMinMax.setVisibility(View.VISIBLE);
-                    dialog.mProgressMinMax.setText("0/" + builder.progressMax);
+                    dialog.mProgressMinMax.setText(String.format(builder.progressNumberFormat,
+                            0, builder.progressMax));
                     ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) dialog.mProgress.getLayoutParams();
                     lp.leftMargin = 0;
                     lp.rightMargin = 0;
                 } else {
                     dialog.mProgressMinMax.setVisibility(View.GONE);
                 }
-                dialog.mProgressLabel.setText("0%");
+                dialog.mProgressLabel.setText(builder.progressPercentFormat.format(0));
             }
         }
     }

+ 48 - 3
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -44,6 +44,7 @@ import com.afollestad.materialdialogs.internal.MDTintHelper;
 import com.afollestad.materialdialogs.util.DialogUtils;
 import com.afollestad.materialdialogs.util.TypefaceHelper;
 
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -412,6 +413,9 @@ public class MaterialDialog extends DialogBase implements
         protected int inputMaxLength = -1;
         protected int inputMaxLengthErrorColor = 0;
 
+        protected String progressNumberFormat;
+        protected NumberFormat progressPercentFormat;
+
         protected boolean titleColorSet = false;
         protected boolean contentColorSet = false;
         protected boolean itemColorSet = false;
@@ -462,6 +466,9 @@ public class MaterialDialog extends DialogBase implements
             this.negativeColor = this.widgetColor;
             this.neutralColor = this.widgetColor;
 
+            this.progressPercentFormat = NumberFormat.getPercentInstance();
+            this.progressNumberFormat = "%1d/%2d";
+
             // Set the default theme based on the Activity theme's primary color darkness (more white or more black)
             final int primaryTextColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
             this.theme = DialogUtils.isColorDark(primaryTextColor) ? Theme.LIGHT : Theme.DARK;
@@ -929,6 +936,24 @@ public class MaterialDialog extends DialogBase implements
             return progress(indeterminate, max);
         }
 
+        /**
+         * hange the format of the small text showing current and maximum units of progress.
+         * The default is "%1d/%2d".
+         */
+        public Builder progressNumberFormat(String format) {
+            this.progressNumberFormat = format;
+            return this;
+        }
+
+        /**
+         * Change the format of the small text showing the percentage of progress.
+         * The default is NumberFormat.getPercentageInstance().
+         */
+        public Builder progressPercentFormat(NumberFormat format) {
+            this.progressPercentFormat = format;
+            return this;
+        }
+
         public Builder widgetColor(@ColorInt int color) {
             this.widgetColor = color;
             this.widgetColorSet = true;
@@ -1353,9 +1378,11 @@ public class MaterialDialog extends DialogBase implements
             @Override
             public void run() {
                 final int percentage = (int) (((float) getCurrentProgress() / (float) getMaxProgress()) * 100f);
-                mProgressLabel.setText(percentage + "%");
-                if (mProgressMinMax != null)
-                    mProgressMinMax.setText(getCurrentProgress() + "/" + getMaxProgress());
+                mProgressLabel.setText(mBuilder.progressPercentFormat.format(percentage));
+                if (mProgressMinMax != null) {
+                    mProgressMinMax.setText(String.format(mBuilder.progressNumberFormat,
+                            getCurrentProgress(), getMaxProgress()));
+                }
             }
         });
     }
@@ -1375,6 +1402,24 @@ public class MaterialDialog extends DialogBase implements
         return mProgress.getMax();
     }
 
+    /**
+     * Change the format of the small text showing the percentage of progress.
+     * The default is NumberFormat.getPercentageInstance().
+     */
+    public final void setProgressPercentFormat(NumberFormat format) {
+        mBuilder.progressPercentFormat = format;
+        setProgress(getCurrentProgress()); // invalidates display
+    }
+
+    /**
+     * Change the format of the small text showing current and maximum units of progress.
+     * The default is "%1d/%2d".
+     */
+    public final void setProgressNumberFormat(String format) {
+        mBuilder.progressNumberFormat = format;
+        setProgress(getCurrentProgress()); // invalidates display
+    }
+
     public final boolean isCancelled() {
         return !isShowing();
     }