浏览代码

Merge pull request #124 from hzsweers/master

Tweak onClick() code to behave like AlertDialog is expected to
Aidan Follestad 10 年之前
父节点
当前提交
826664d92d

+ 52 - 0
README.md

@@ -299,6 +299,58 @@ it to display the action buttons below your list, however this is only useful in
 
 ---
 
+### Comprehensive Listener APIs
+
+There are many listeners available, and they can all be used independently to give you fine-grained control
+over the behavior of the dialog.
+
+```java
+new MaterialDialog.Builder(this)
+        .title(R.string.complex)
+        .positiveText("Yes")
+        .negativeText("No")
+        .neutralText("Maybe")
+        .items(R.array.socialNetworks)
+        .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallback() {
+            @Override
+            public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
+                Toast.makeText(MainActivity.this, "Clicked " + text, Toast.LENGTH_SHORT).show();
+            }
+        })
+        .callback(new MaterialDialog.FullCallback() {
+            @Override
+            public void onNeutral(MaterialDialog dialog) {
+                Toast.makeText(MainActivity.this, "Maybe", Toast.LENGTH_SHORT).show();
+            }
+
+            @Override
+            public void onNegative(MaterialDialog dialog) {
+                Toast.makeText(MainActivity.this, "No", Toast.LENGTH_SHORT).show();
+            }
+
+            @Override
+            public void onPositive(MaterialDialog dialog) {
+                Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show();
+            }
+        })
+        .cancelListener(new DialogInterface.OnCancelListener() {
+            @Override
+            public void onCancel(DialogInterface dialog) {
+                Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
+            }
+        })
+        .show();
+```
+
+If you want to preselect item(s), pass an array of indices in place of null in `itemsCallbackSingleChoice()`.
+For an example, `new Integer[] { 2, 5 }`. If `autoDismiss` is turned off, then you must manually
+dismiss the dialog in the callback. Auto dismiss is on by default. When action buttons are not added, the
+callback will be called every time you select an item since no action is available to press, without the
+dialog being dismissed. You can pass `positiveText()` or the other action buttons to the builder to force
+it to display the action buttons below your list, however this is only useful in some specific cases.
+
+---
+
 ### Custom List Dialogs
 
 Like Android's native dialogs, you can also pass in your own adapter via `.adapter()` to customize

+ 32 - 40
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -561,52 +561,44 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
     @Override
     public final void onClick(View v) {
         String tag = (String) v.getTag();
-        if (tag.equals(POSITIVE)) {
-            if (listCallbackSingle != null) {
-                if (autoDismiss) dismiss();
-                sendSingleChoiceCallback(v);
-            } else if (listCallbackMulti != null) {
-                if (autoDismiss) dismiss();
-                sendMultichoiceCallback();
-            } else if (callback != null) {
-                if (autoDismiss) dismiss();
-                callback.onPositive(this);
-            } else if (autoDismiss) dismiss();
-        } else if (tag.equals(NEGATIVE)) {
-            if (callback != null && callback instanceof Callback) {
+        switch (tag) {
+            case POSITIVE:
+                if (callback != null) {
+                    callback.onPositive(this);
+                }
                 if (autoDismiss) dismiss();
-                ((Callback) callback).onNegative(this);
-            } else if (autoDismiss) dismiss();
-        } else if (tag.equals(NEUTRAL)) {
-            if (callback != null && callback instanceof FullCallback) {
+                break;
+            case NEGATIVE:
+                if (callback != null && callback instanceof Callback) {
+                    ((Callback) callback).onNegative(this);
+                }
                 if (autoDismiss) dismiss();
-                ((FullCallback) callback).onNeutral(this);
-            } else if (autoDismiss) dismiss();
-        } else {
-            String[] split = tag.split(":");
-            int index = Integer.parseInt(split[0]);
-            if (listCallback != null) {
+                break;
+            case NEUTRAL:
+                if (callback != null && callback instanceof FullCallback) {
+                    ((FullCallback) callback).onNeutral(this);
+                }
                 if (autoDismiss) dismiss();
-                listCallback.onSelection(this, v, index, split[1]);
-            } else if (listCallbackSingle != null) {
-                RadioButton cb = (RadioButton) ((LinearLayout) v).getChildAt(0);
-                if (!cb.isChecked())
-                    cb.setChecked(true);
-                invalidateSingleChoice(index);
-                if (positiveText == null) {
-                    // Immediately send the selection callback if no positive button is shown
+                break;
+            default:
+                String[] split = tag.split(":");
+                int index = Integer.parseInt(split[0]);
+                if (listCallback != null) {
                     if (autoDismiss) dismiss();
-                    sendSingleChoiceCallback(v);
-                }
-            } else if (listCallbackMulti != null) {
-                CheckBox cb = (CheckBox) ((LinearLayout) v).getChildAt(0);
-                cb.setChecked(!cb.isChecked());
-                if (positiveText == null) {
-                    // Immediately send the selection callback if no positive button is shown
+                    listCallback.onSelection(this, v, index, split[1]);
+                } else if (listCallbackSingle != null) {
+                    RadioButton cb = (RadioButton) ((LinearLayout) v).getChildAt(0);
+                    if (!cb.isChecked())
+                        cb.setChecked(true);
+                    invalidateSingleChoice(index);
                     if (autoDismiss) dismiss();
+                    sendSingleChoiceCallback(v);
+                } else if (listCallbackMulti != null) {
+                    CheckBox cb = (CheckBox) ((LinearLayout) v).getChildAt(0);
+                    cb.setChecked(!cb.isChecked());
                     sendMultichoiceCallback();
-                }
-            } else if (autoDismiss) dismiss();
+                } else if (autoDismiss) dismiss();
+                break;
         }
     }
 

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

@@ -119,6 +119,13 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
             }
         });
 
+        findViewById(R.id.complex).setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                showComplexListeners();
+            }
+        });
+
         findViewById(R.id.customListItems).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -311,6 +318,44 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                 .show();
     }
 
+    private void showComplexListeners() {
+        new MaterialDialog.Builder(this)
+                .title(R.string.complex)
+                .positiveText("Yes")
+                .negativeText("No")
+                .neutralText("Maybe")
+                .items(R.array.socialNetworks)
+                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallback() {
+                    @Override
+                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
+                        Toast.makeText(MainActivity.this, "Clicked " + text, Toast.LENGTH_SHORT).show();
+                    }
+                })
+                .callback(new MaterialDialog.FullCallback() {
+                    @Override
+                    public void onNeutral(MaterialDialog dialog) {
+                        Toast.makeText(MainActivity.this, "Maybe", Toast.LENGTH_SHORT).show();
+                    }
+
+                    @Override
+                    public void onNegative(MaterialDialog dialog) {
+                        Toast.makeText(MainActivity.this, "No", Toast.LENGTH_SHORT).show();
+                    }
+
+                    @Override
+                    public void onPositive(MaterialDialog dialog) {
+                        Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show();
+                    }
+                })
+                .cancelListener(new DialogInterface.OnCancelListener() {
+                    @Override
+                    public void onCancel(DialogInterface dialog) {
+                        Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
+                    }
+                })
+                .show();
+    }
+
     private void showCustomList() {
         MaterialDialog dialog = new MaterialDialog.Builder(this)
                 .title(R.string.socialNetworks)

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

@@ -93,6 +93,13 @@
             android:text="@string/multiChoice"
             android:layout_marginTop="@dimen/sample_button_spacing" />
 
+        <Button
+            android:id="@+id/complex"
+            android:layout_width="match_parent"
+            android:layout_height="56dp"
+            android:text="@string/complex"
+            android:layout_marginTop="@dimen/sample_button_spacing" />
+
         <Button
             android:id="@+id/customListItems"
             android:layout_width="match_parent"

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

@@ -13,6 +13,7 @@
     <string name="list">Basic List</string>
     <string name="singleChoice">Single Choice</string>
     <string name="multiChoice">Multi Choice</string>
+    <string name="complex">Complex Listeners</string>
     <string name="customView">Custom View</string>
     <string name="themed">Themed</string>
     <string name="signalStrength">Signal strength</string>