Bläddra i källkod

inputMaxLength() was deprecated in favor of new inputRange()

Aidan Follestad 10 år sedan
förälder
incheckning
eade45cbd6

+ 16 - 15
README.md

@@ -826,26 +826,27 @@ There's also a global theming attribute as shown in the Global Theming section o
 ## Limiting Input Length
 
 The code below will show a little indicator in the input dialog that tells the user how many characters they've
-typed, and how many more they can type before reaching a certain limit. If they go over that limit,
-the dialog won't allow them to submit the input. It will also color the input field and length indicator
-with an error color of your choosing (or the default if you don't specify one).
+typed. If they type less than 2 characters, or more than 20, the dialog won't allow the input to be submitted.
+It will also color the input field and character counter in error color passed for the third parameter.
+ 
+If you pass 0 for the min length, there will be no min length. If you pass -1 for the max length, there will
+be no max length. If you don't pass a third parameter at all, it will default to Material red. 
 
 ```java
 new MaterialDialog.Builder(this)
-        .title(R.string.input)
-        .inputMaxLengthRes(20, R.color.material_red_500)
-        .input(null, null, new MaterialDialog.InputCallback() {
-            @Override
-            public void onInput(MaterialDialog dialog, CharSequence input) {
-                // Do something
-            }
-        }).show();
+    .title(R.string.input)
+    .inputRangeRes(2, 20, R.color.material_red_500)
+    .input(null, null, new MaterialDialog.InputCallback() {
+        @Override
+        public void onInput(MaterialDialog dialog, CharSequence input) {
+            // Do something
+        }
+    }).show();
 ```
 
-*Note that `inputMaxLengthRes(int, int)` takes a color resource ID for the second parameter, while
-`inputMaxLength(int, int)` takes a literal color integer for the second parameter. You can use either one.
-If you want to use the default error color from the guidelines, you can use `inputMaxLength(int)` which doesn't
-take the second error color parameter*
+*Note that `inputRangeRes(int, int, int)` takes a color resource ID for the third parameter, while
+`inputRange(int, int, int)` takes a literal color integer for the second parameter. You can use either one, or use
+the variation that doesn't take a third parameter at all.
 
 ## Custom Invalidation
 

+ 1 - 1
core/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -432,7 +432,7 @@ class DialogInit {
         }
 
         dialog.inputMinMax = (TextView) dialog.view.findViewById(R.id.minMax);
-        if (builder.inputMaxLength > -1) {
+        if (builder.inputMinLength > 0 || builder.inputMaxLength > -1) {
             dialog.invalidateInputMinMaxIndicator(dialog.input.getText().toString().length(),
                     !builder.inputAllowEmpty);
         } else {

+ 49 - 13
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -15,6 +15,7 @@ import android.support.annotation.ColorInt;
 import android.support.annotation.ColorRes;
 import android.support.annotation.DimenRes;
 import android.support.annotation.DrawableRes;
+import android.support.annotation.IntRange;
 import android.support.annotation.LayoutRes;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
@@ -422,8 +423,9 @@ public class MaterialDialog extends DialogBase implements
         protected boolean inputAllowEmpty;
         protected int inputType = -1;
         protected boolean alwaysCallInputCallback;
+        protected int inputMinLength = -1;
         protected int inputMaxLength = -1;
-        protected int inputMaxLengthErrorColor = 0;
+        protected int inputRangeErrorColor = 0;
 
         protected String progressNumberFormat;
         protected NumberFormat progressPercentFormat;
@@ -1143,30 +1145,62 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
-        public Builder inputMaxLength(int maxLength) {
-            return inputMaxLength(maxLength, 0);
+        /**
+         * @deprecated in favor of {@link #inputRange(int, int)}
+         */
+        @Deprecated
+        public Builder inputMaxLength(@IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength) {
+            return inputRange(0, maxLength, 0);
+        }
+
+        /**
+         * @deprecated in favor of {@link #inputRange(int, int, int)}
+         */
+        @Deprecated
+        public Builder inputMaxLength(@IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength, @ColorInt int errorColor) {
+            return inputRange(0, maxLength, errorColor);
+        }
+
+        /**
+         * @deprecated in favor of {@link #inputRangeRes(int, int, int)}
+         */
+        @Deprecated
+        public Builder inputMaxLengthRes(@IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength, @ColorRes int errorColor) {
+            return inputRangeRes(0, maxLength, errorColor);
+        }
+
+        public Builder inputRange(@IntRange(from = 0, to = Integer.MAX_VALUE) int minLength,
+                                  @IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength) {
+            return inputRange(minLength, maxLength, 0);
         }
 
         /**
          * @param errorColor Pass in 0 for the default red error color (as specified in guidelines).
          */
-        public Builder inputMaxLength(int maxLength, int errorColor) {
+        public Builder inputRange(@IntRange(from = 0, to = Integer.MAX_VALUE) int minLength,
+                                  @IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength,
+                                  @ColorInt int errorColor) {
+            if (minLength < 0)
+                throw new IllegalArgumentException("Min length for input dialogs cannot be less than 0.");
             if (maxLength < 1)
                 throw new IllegalArgumentException("Max length for input dialogs cannot be less than 1.");
+            this.inputMinLength = minLength;
             this.inputMaxLength = maxLength;
             if (errorColor == 0) {
-                inputMaxLengthErrorColor = ContextCompat.getColor(context, R.color.md_edittext_error);
+                this.inputRangeErrorColor = ContextCompat.getColor(context, R.color.md_edittext_error);
             } else {
-                this.inputMaxLengthErrorColor = errorColor;
+                this.inputRangeErrorColor = errorColor;
             }
             return this;
         }
 
         /**
-         * Same as #{@link #inputMaxLength(int, int)}, but it takes a color resource ID for the error color.
+         * Same as #{@link #inputRange(int, int, int)}, but it takes a color resource ID for the error color.
          */
-        public Builder inputMaxLengthRes(int maxLength, @ColorRes int errorColor) {
-            return inputMaxLength(maxLength, ContextCompat.getColor(context, errorColor));
+        public Builder inputRangeRes(@IntRange(from = 0, to = Integer.MAX_VALUE) int minLength,
+                                     @IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength,
+                                     @ColorRes int errorColor) {
+            return inputRange(minLength, maxLength, ContextCompat.getColor(context, errorColor));
         }
 
         public Builder alwaysCallInputCallback() {
@@ -1584,10 +1618,12 @@ public class MaterialDialog extends DialogBase implements
 
     protected void invalidateInputMinMaxIndicator(int currentLength, boolean emptyDisabled) {
         if (inputMinMax != null) {
-            inputMinMax.setText(currentLength + "/" + mBuilder.inputMaxLength);
-            final boolean isDisabled = (emptyDisabled && currentLength == 0) || currentLength > mBuilder.inputMaxLength;
-            final int colorText = isDisabled ? mBuilder.inputMaxLengthErrorColor : mBuilder.contentColor;
-            final int colorWidget = isDisabled ? mBuilder.inputMaxLengthErrorColor : mBuilder.widgetColor;
+            inputMinMax.setText(String.format("%d/%d", currentLength, mBuilder.inputMaxLength));
+            final boolean isDisabled = (emptyDisabled && currentLength == 0) ||
+                    currentLength > mBuilder.inputMaxLength ||
+                    currentLength < mBuilder.inputMinLength;
+            final int colorText = isDisabled ? mBuilder.inputRangeErrorColor : mBuilder.contentColor;
+            final int colorWidget = isDisabled ? mBuilder.inputRangeErrorColor : mBuilder.widgetColor;
             inputMinMax.setTextColor(colorText);
             MDTintHelper.setTint(input, colorWidget);
             final View positiveAb = getActionButton(DialogAction.POSITIVE);

+ 1 - 1
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -664,7 +664,7 @@ public class MainActivity extends AppCompatActivity implements
                 .inputType(InputType.TYPE_CLASS_TEXT |
                         InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
                         InputType.TYPE_TEXT_FLAG_CAP_WORDS)
-                .inputMaxLength(16)
+                .inputRange(2, 16)
                 .positiveText(R.string.submit)
                 .input(R.string.input_hint, R.string.input_hint, false, new MaterialDialog.InputCallback() {
                     @Override