欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DialogFragmentUtil工具类

程序员文章站 2022-05-16 11:26:19
...

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;

public class DialogFragmentUtil {

    public static void showSingleDialog(AppCompatActivity 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(AppCompatActivity 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();
        }
    }
}

DialogFragmentUtil工具类

往哪里看到的了,侵删

相关标签: 安卓相关