DialogUtils Material风格对话框工具类

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.DialogInterface.OnShowListener;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.afollestad.materialdialogs.MaterialDialog.Builder;
import com.afollestad.materialdialogs.MaterialDialog.InputCallback;
import com.afollestad.materialdialogs.MaterialDialog.ListCallback;
import com.afollestad.materialdialogs.MaterialDialog.ListCallbackMultiChoice;
import com.afollestad.materialdialogs.MaterialDialog.ListCallbackSingleChoice;
import com.afollestad.materialdialogs.MaterialDialog.SingleButtonCallback;
import java.util.List;
import me.goldze.mvvmhabit.R.color;
import me.goldze.mvvmhabit.R.drawable;
import me.goldze.mvvmhabit.R.string;

public class MaterialDialogUtils {
    public MaterialDialogUtils() {
    }

    public void showThemed(Context context, String title, String content) {
        (new Builder(context)).title(title).content(content).positiveText("agree").negativeText("disagree").positiveColorRes(color.white).negativeColorRes(color.white).titleGravity(GravityEnum.CENTER).titleColorRes(color.white).contentColorRes(17170443).backgroundColorRes(color.material_blue_grey_800).dividerColorRes(color.white).btnSelector(drawable.md_selector, DialogAction.POSITIVE).positiveColor(-1).negativeColorAttr(16842810).theme(Theme.DARK).autoDismiss(true).showListener(new OnShowListener() {
            public void onShow(DialogInterface dialog) {
            }
        }).cancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
            }
        }).dismissListener(new OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
            }
        }).show();
    }

    public static Builder showIndeterminateProgressDialog(Context context, String content, boolean horizontal) {
        Builder builder = (new Builder(context)).title(content).progress(true, 0).progressIndeterminateStyle(horizontal).canceledOnTouchOutside(false).widgetColorRes(color.colorPrimary).backgroundColorRes(color.white).keyListener(new OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(event.getAction() == 0 && keyCode == 4) {
                    ;
                }

                return false;
            }
        });
        return builder;
    }

    public static Builder showBasicDialog(Context context, String content) {
        Builder builder = (new Builder(context)).title(content).positiveText("确定").negativeText("取消");
        return builder;
    }

    public static Builder showBasicDialogNoTitle(Context context, String content) {
        Builder builder = (new Builder(context)).content(content).positiveText("确定").negativeText("取消");
        return builder;
    }

    public static Builder showBasicDialogNoCancel(Context context, String title, String content) {
        Builder builder = (new Builder(context)).title(title).content(content).positiveText("确定");
        return builder;
    }

    public static Builder showBasicDialog(Context context, String title, String content) {
        Builder builder = (new Builder(context)).title(title).content(content).positiveText("确定").negativeText("取消");
        return builder;
    }

    public static Builder showBasicDialogPositive(Context context, String title, String content) {
        Builder builder = (new Builder(context)).title(title).content(content).positiveText("复制").negativeText("取消");
        return builder;
    }

    public static Builder getSelectDialog(Context context, String title, String[] arrays) {
        Builder builder = (new Builder(context)).items(arrays).itemsColor(-12226906).negativeText("取消");
        if(!TextUtils.isEmpty(title)) {
            builder.title(title);
        }

        return builder;
    }

    public static Builder showBasicListDialog(Context context, String title, List content) {
        Builder builder = (new Builder(context)).title(title).items(content).itemsCallback(new ListCallback() {
            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
            }
        }).negativeText("取消");
        return builder;
    }

    public static Builder showSingleListDialog(Context context, String title, List content) {
        Builder builder = (new Builder(context)).title(title).items(content).itemsCallbackSingleChoice(1, new ListCallbackSingleChoice() {
            public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                return true;
            }
        }).positiveText("选择");
        return builder;
    }

    public static Builder showMultiListDialog(Context context, String title, List content) {
        Builder builder = (new Builder(context)).title(title).items(content).itemsCallbackMultiChoice(new Integer[]{Integer.valueOf(1), Integer.valueOf(3)}, new ListCallbackMultiChoice() {
            public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                return true;
            }
        }).onNeutral(new SingleButtonCallback() {
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.clearSelectedIndices();
            }
        }).alwaysCallMultiChoiceCallback().positiveText(string.md_choose_label).autoDismiss(false).neutralText("clear").itemsDisabledIndices(new Integer[]{Integer.valueOf(0), Integer.valueOf(1)});
        return builder;
    }

    public static void showCustomDialog(Context context, String title, int content) {
        MaterialDialog dialog = (new Builder(context)).title(title).customView(content, true).positiveText("确定").negativeText(17039360).onPositive(new SingleButtonCallback() {
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            }
        }).build();
    }

    public static Builder showInputDialog(Context context, String title, String content) {
        Builder builder = (new Builder(context)).title(title).content(content).inputType(8289).positiveText("确定").negativeText("取消").input("hint", "prefill", true, new InputCallback() {
            public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
            }
        });
        return builder;
    }
}

你可能感兴趣的:(android)