Explorar o código

Added info and a sample of custom invalidation in input dialogs.

Aidan Follestad %!s(int64=10) %!d(string=hai) anos
pai
achega
9df53fa7fb

+ 6 - 2
README.md

@@ -804,8 +804,12 @@ take the second error color parameter*
 
 
 The easiest way to invalidate (enable or disable the EditText based on whether you think the input is acceptable)
 The easiest way to invalidate (enable or disable the EditText based on whether you think the input is acceptable)
 input dialogs is to call `alwaysCallInputCallback()` from the `Builder` so that the callback is invoked
 input dialogs is to call `alwaysCallInputCallback()` from the `Builder` so that the callback is invoked
-every time the user changes their input. From there, you can constantly check what they've typed
-and use `setEnabled()` on the `EditText` view (which can be retrieved with `MaterialDialog#getInputEditText()`).
+every time the user changes their input. From there, you can constantly check what they've typed. If you
+decide they shouldn't be able to submit that, you can disable the submit button using this from within the callback:
+
+```java
+dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
+```
 
 
 ---
 ---
 
 

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

@@ -220,6 +220,13 @@ public class MainActivity extends AppCompatActivity implements
             }
             }
         });
         });
 
 
+        findViewById(R.id.input_custominvalidation).setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                showInputDialogCustomInvalidation();
+            }
+        });
+
         findViewById(R.id.progress1).setOnClickListener(new View.OnClickListener() {
         findViewById(R.id.progress1).setOnClickListener(new View.OnClickListener() {
             @Override
             @Override
             public void onClick(View v) {
             public void onClick(View v) {
@@ -635,6 +642,29 @@ public class MainActivity extends AppCompatActivity implements
                 }).show();
                 }).show();
     }
     }
 
 
+    private void showInputDialogCustomInvalidation() {
+        new MaterialDialog.Builder(this)
+                .title(R.string.input)
+                .content(R.string.input_content_custominvalidation)
+                .inputType(InputType.TYPE_CLASS_TEXT |
+                        InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
+                        InputType.TYPE_TEXT_FLAG_CAP_WORDS)
+                .positiveText(R.string.submit)
+                .alwaysCallInputCallback() // this forces the callback to be invoked with every input change
+                .input(R.string.input_hint, 0, false, new MaterialDialog.InputCallback() {
+                    @Override
+                    public void onInput(MaterialDialog dialog, CharSequence input) {
+                        if (input.toString().equalsIgnoreCase("hello")) {
+                            dialog.setContent("I told you not to type that!");
+                            dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
+                        } else {
+                            dialog.setContent(R.string.input_content_custominvalidation);
+                            dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
+                        }
+                    }
+                }).show();
+    }
+
     private void showProgressDialog(boolean indeterminate, boolean horizontalIndeterminate) {
     private void showProgressDialog(boolean indeterminate, boolean horizontalIndeterminate) {
         if (indeterminate) {
         if (indeterminate) {
             new MaterialDialog.Builder(this)
             new MaterialDialog.Builder(this)

+ 7 - 0
sample/src/main/res/layout/activity_main.xml

@@ -163,6 +163,13 @@
             android:layout_marginTop="@dimen/sample_button_spacing"
             android:layout_marginTop="@dimen/sample_button_spacing"
             android:text="@string/input" />
             android:text="@string/input" />
 
 
+        <Button
+            android:id="@+id/input_custominvalidation"
+            android:layout_width="match_parent"
+            android:layout_height="56dp"
+            android:layout_marginTop="@dimen/sample_button_spacing"
+            android:text="@string/input_custominvalidation" />
+
         <Button
         <Button
             android:id="@+id/progress1"
             android:id="@+id/progress1"
             android:layout_width="match_parent"
             android:layout_width="match_parent"

+ 2 - 0
sample/src/main/res/values/strings.xml

@@ -150,6 +150,7 @@
     <!-- Optional button to Skip a PreferenceActivity [CHAR LIMIT=20] -->
     <!-- Optional button to Skip a PreferenceActivity [CHAR LIMIT=20] -->
     <string name="skip_button_label">Skip</string>
     <string name="skip_button_label">Skip</string>
     <string name="input">Input</string>
     <string name="input">Input</string>
+    <string name="input_custominvalidation">Input (Custom Invalidation)</string>
     <string name="input_hint">John Appleseed</string>
     <string name="input_hint">John Appleseed</string>
     <string name="input_content">What\'s your name?</string>
     <string name="input_content">What\'s your name?</string>
     <string name="simple_list">Simple Dialog (Guidelines Spec)</string>
     <string name="simple_list">Simple Dialog (Guidelines Spec)</string>
@@ -160,5 +161,6 @@
     <string name="preference_dialog_title">Dialog Title</string>
     <string name="preference_dialog_title">Dialog Title</string>
     <string name="material_dialog_pref_title">Dialog Preference</string>
     <string name="material_dialog_pref_title">Dialog Preference</string>
     <string name="material_dialog_pref_summary">This is an example of a basic dialog preference that automatically uses Material Dialogs to show the dialog.</string>
     <string name="material_dialog_pref_summary">This is an example of a basic dialog preference that automatically uses Material Dialogs to show the dialog.</string>
+    <string name="input_content_custominvalidation">Do not type hello, or else!</string>
 
 
 </resources>
 </resources>