DialogFragment工具类

注:这里传进来的activity是v4的。

public class DialogUtil {

    public static void showSingleDialog(FbActivity activity, String title, String msg, String positive,
                                        DialogInterface.OnClickListener listener) {
        createSingleDialog(title, msg, positive, listener)
                .show(activity.getSupportFragmentManager(), activity.getClass().getSimpleName() + "Dialog");
    }

    public static void showDoubleDialog(FbActivity activity, String title, String msg, String positive,
                                        String negative, DialogInterface.OnClickListener listener) {
        createDoubleDialog(title, msg, positive, negative, listener)
                .show(activity.getSupportFragmentManager(), activity.getClass().getSimpleName() + "Dialog");
    }


    public static DialogFragment createSingleDialog(String title, String msg, String positive,
                                                    DialogInterface.OnClickListener listener) {
        return AlertDialogFragment.newSingleInstance(title, msg, positive, listener);
    }

    public static DialogFragment createDoubleDialog(String title, String msg, String positive,
                                                    String negative, DialogInterface.OnClickListener listener) {
        return AlertDialogFragment.newDoubleInstance(title, msg, positive, negative, listener);
    }

    public static class AlertDialogFragment extends DialogFragment {

        private static DialogInterface.OnClickListener mListener;

        public static AlertDialogFragment newSingleInstance(String title, String msg, String positive,
                                                            DialogInterface.OnClickListener listener) {
            return newDoubleInstance(title, msg, positive, null, listener);
        }

        public static AlertDialogFragment newDoubleInstance(String title, String msg, String positive,
                                                            String negative, DialogInterface.OnClickListener listener) {
            AlertDialogFragment frag = new AlertDialogFragment();
            Bundle args = new Bundle();
            args.putString("title", title);
            args.putString("msg", msg);
            args.putString("negative", negative);
            args.putString("positive", positive);
            mListener = listener;
            frag.setArguments(args);
            return frag;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String title = getArguments().getString("title");
            String msg = getArguments().getString("msg");
            String positive = getArguments().getString("positive");
            String negative = getArguments().getString("negative");

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(title)
                    .setMessage(msg)
                    .setPositiveButton(positive, mListener);

            if (negative != null) {
                builder.setNegativeButton(negative, mListener);
            }
            return builder.create();
        }
    }
}

你可能感兴趣的:(DialogFragment工具类)