Skip to content

Instantly share code, notes, and snippets.

@toybeans
Last active November 10, 2015 00:49
Show Gist options
  • Save toybeans/39e8d062c529305f8299 to your computer and use it in GitHub Desktop.
Save toybeans/39e8d062c529305f8299 to your computer and use it in GitHub Desktop.
タイトルとメッセージ、コールバックを設定できるダイアログ
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
/**
* Created by ry0watana6e on 2015/10/19.
*/
public class CallbackDialogFragment extends DialogFragment {
private String title;
private String message;
private OnCallbackListener mCallback;
public interface OnCallbackListener {
void onClickPositive();
void onClickNegative();
}
public static CallbackDialogFragment newInstance(String title, String message) {
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message" , message);
CallbackDialogFragment fragment = new CallbackDialogFragment();
fragment.setArguments(args);
return fragment;
}
public void setCallbackListenr(OnCallbackListener _callback){
mCallback = _callback;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if(getArguments() != null){
title = getArguments().getString("title");
message = getArguments().getString("message");
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
if(isExist(title))
builder.setTitle(title);
if(isExist(message))
builder.setMessage(message);
builder.setPositiveButton("はい", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(null != mCallback)
mCallback.onClickPositive();
}
});
builder.setNegativeButton("いいえ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(null != mCallback)
mCallback.onClickNegative();
}
});
return builder.create();
}
private boolean isExist(String val) {
if(val == null) return false;
if(val.length() == 0) return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment