Browse Source

Tweak onClick() code to behave like AlertDialog is expected to

Couple things:
- List clicks and button clicks don't interfere with each other
- Multichoice clicks shouldn't autodismiss

This is more realistic behavior for an alert dialog with many listener interfaces. This should also not interfere with existing implementations (and fix it for anyone that needs the listeners to behave separately)
Henri Sweers 10 years ago
parent
commit
09f52ebb33

+ 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;
         }
     }