Skip to content

Instantly share code, notes, and snippets.

@neiraza
Created July 25, 2013 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neiraza/6076893 to your computer and use it in GitHub Desktop.
Save neiraza/6076893 to your computer and use it in GitHub Desktop.
DialogFragmentを使って、汎用的に使えそうなAlertDialogをつくってみた
/**
* Created by togu on 2013/07/25.
*/
public class CommonDialogFragment extends DialogFragment {
private DialogListener listener = null;
public static CommonDialogFragment newInstance(String title, String message) {
return setCommonDialogFragment(0, title, message);
}
public static CommonDialogFragment newInstance(int iconId, String title, String message) {
return setCommonDialogFragment(iconId, title, message);
}
private static CommonDialogFragment setCommonDialogFragment(int iconId, String title, String message) {
CommonDialogFragment frag = new CommonDialogFragment();
Bundle bundle = new Bundle();
bundle.putInt("icon", iconId == 0 ? R.drawable.status_icon : iconId);
bundle.putString("title", title);
bundle.putString("message", message);
frag.setArguments(bundle);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
int iconId = bundle.getInt("icon");
String title = bundle.getString("title");
String message = bundle.getString("message");
return new AlertDialog.Builder(getActivity())
.setIcon(iconId)
.setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onPositiveClick(getDialogTag());
}
}
)
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onNegativeClick(getDialogTag());
}
}
)
.create();
}
public String getDialogTag() {
return getTag() != null ? getTag() : "";
}
/**
* DialogListener
*
* @param listener
*/
public void setDialogListener(DialogListener listener) {
this.listener = listener;
}
/**
* DialogListener
*/
public void removeDialogListener() {
this.listener = null;
}
}
/**
* Created by togu on 2013/07/25.
*/
public interface DialogListener extends EventListener {
public void onPositiveClick(String tag);
public void onNegativeClick(String tag);
}
public class MainActivity extends FragmentActivity implements OnClickListener, DialogListener {
/*
Alert Tag
どこのボタンでアラートでたか分かるようにしたくて、ただ...逢いたくて
*/
private static final String TAG_MISAKA_20001 = "sisters_misaka_20001_last_order";
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
//適当、アラートを出したいボタンを定義とか
}
public void onResume() {
super.onResume();
//適当
}
public void onClick(View v) {
switch (v.getId()) {
//ボタンをタップした時にアラートを出す
case R.id.hoge:
CommonDialogFragment alertDialogFragment = CommonDialogFragment.newInstance(
"夏だし・・・・", //alert dialog title
"( ゚∀゚)o彡°おっぱい!おっぱい!"; //alert dialog message
alertDialogFragment.setDialogListener(this);
alertDialogFragment.show(getSupportFragmentManager(), TAG_MISAKA_20001);
break;
}
}
/**
* DialogListener
*/
@Override
public void onPositiveClick(String tag) {
if (TAG_MISAKA_20001.equals(tag)) {
Toast.makeText(cxt, "okてことで", Toast.LENGTH_SHORT).show();
}
}
/**
* DialogListener
*/
@Override
public void onNegativeClick(String tag) {
if (TAG_MISAKA_20001.equals(tag)) {
Toast.makeText(cxt, "cancelしちゃうわ", Toast.LENGTH_SHORT).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment