Skip to content

Instantly share code, notes, and snippets.

@takke
Created April 26, 2015 05:03
Show Gist options
  • Save takke/72e06965c737646e830e to your computer and use it in GitHub Desktop.
Save takke/72e06965c737646e830e to your computer and use it in GitHub Desktop.
AlertDialogのクソラッパー
package jp.takke.ui;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MyAlertDialog {
public static boolean sUseOriginalDialog = false;
private final android.support.v7.app.AlertDialog sd;
private final android.app.AlertDialog ad;
public MyAlertDialog(android.support.v7.app.AlertDialog alertDialog) {
sd = alertDialog;
ad = null;
}
public MyAlertDialog(android.app.AlertDialog alertDialog) {
sd = null;
ad = alertDialog;
}
public void show() {
if (sd != null) {
sd.show();
} else {
ad.show();
}
}
public ListView getListView() {
if (sd != null) {
return sd.getListView();
} else {
return ad.getListView();
}
}
public void dismiss() {
if (sd != null) {
sd.dismiss();
} else {
ad.dismiss();
}
}
public Window getWindow() {
if (sd != null) {
return sd.getWindow();
} else {
return ad.getWindow();
}
}
public Button getButton(int i) {
if (sd != null) {
return sd.getButton(i);
} else {
return ad.getButton(i);
}
}
public View findViewById(int id) {
if (sd != null) {
return sd.findViewById(id);
} else {
return ad.findViewById(id);
}
}
public static class Builder {
final android.support.v7.app.AlertDialog.Builder sb; // Support's AlertDialog Builder
final android.app.AlertDialog.Builder ab; // AlertDialog Builder
public Builder(Context context) {
if (sUseOriginalDialog) {
ab = new android.app.AlertDialog.Builder(context);
sb = null;
} else {
ab = null;
sb = new android.support.v7.app.AlertDialog.Builder(context);
}
}
public Builder setTitle(int title) {
if (ab != null) {
ab.setTitle(title);
} else {
sb.setTitle(title);
}
return this;
}
public Builder setTitle(CharSequence sequence) {
if (ab != null) {
ab.setTitle(sequence);
} else {
sb.setTitle(sequence);
}
return this;
}
public Builder setMessage(int id) {
if (ab != null) {
ab.setMessage(id);
} else {
sb.setMessage(id);
}
return this;
}
public Builder setMessage(CharSequence s) {
if (ab != null) {
ab.setMessage(s);
} else {
sb.setMessage(s);
}
return this;
}
public Builder setPositiveButton(int id, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setPositiveButton(id, onClickListener);
} else {
sb.setPositiveButton(id, onClickListener);
}
return this;
}
public Builder setPositiveButton(CharSequence cs, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setPositiveButton(cs, onClickListener);
} else {
sb.setPositiveButton(cs, onClickListener);
}
return this;
}
public Builder setCancelable(boolean cancelable) {
if (ab != null) {
ab.setCancelable(cancelable);
} else {
sb.setCancelable(cancelable);
}
return this;
}
public Builder show() {
if (ab != null) {
ab.show();
} else {
sb.show();
}
return this;
}
public Builder setIcon(int id) {
if (ab != null) {
ab.setIcon(id);
} else {
sb.setIcon(id);
}
return this;
}
public Builder setIcon(Drawable drawable) {
if (ab != null) {
ab.setIcon(drawable);
} else {
sb.setIcon(drawable);
}
return this;
}
public Builder setNegativeButton(int id, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setNegativeButton(id, onClickListener);
} else {
sb.setNegativeButton(id, onClickListener);
}
return this;
}
public Builder setNegativeButton(CharSequence cs, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setNegativeButton(cs, onClickListener);
} else {
sb.setNegativeButton(cs, onClickListener);
}
return this;
}
public Builder setAdapter(ListAdapter adapter, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setAdapter(adapter, onClickListener);
} else {
sb.setAdapter(adapter, onClickListener);
}
return this;
}
public MyAlertDialog create() {
if (ab != null) {
return new MyAlertDialog(ab.create());
} else {
return new MyAlertDialog(sb.create());
}
}
public Builder setItems(CharSequence[] items, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setItems(items, onClickListener);
} else {
sb.setItems(items, onClickListener);
}
return this;
}
public Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
if (ab != null) {
ab.setOnCancelListener(onCancelListener);
} else {
sb.setOnCancelListener(onCancelListener);
}
return this;
}
public Builder setView(View layout) {
if (ab != null) {
ab.setView(layout);
} else {
sb.setView(layout);
}
return this;
}
public Builder setNeutralButton(int id, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setNeutralButton(id, onClickListener);
} else {
sb.setNeutralButton(id, onClickListener);
}
return this;
}
public Builder setNeutralButton(CharSequence cs, DialogInterface.OnClickListener onClickListener) {
if (ab != null) {
ab.setNeutralButton(cs, onClickListener);
} else {
sb.setNeutralButton(cs, onClickListener);
}
return this;
}
public Builder setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) {
if (ab != null) {
ab.setOnKeyListener(onKeyListener);
} else {
sb.setOnKeyListener(onKeyListener);
}
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment