Newer
Older
VipVideo / app / src / main / java / me / bello / viptv / util / DialogBuilder.java
bello on 11 Feb 2021 7 KB key
package me.bello.viptv.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import me.bello.viptv.R;


/**
 * @Info 通用对话框
 * @Author Bello
 * @Time 20-3-17 下午2:38
 * @Ver
 */
public final class DialogBuilder {
    private Context mContext;
    private String titleStr;        // 标题内家
    private String contentStr;      // 提示内容
    private String rightTxt;          // 确定按钮文字
    private String leftTxt;           // 取消按钮文字
    private int titleTxtColor;      // 标题颜色
    private int contentTxtColor;    // 内容颜色
    private int rightTxtColor;        // 确定文字颜色
    private int leftTxtColor;         // 取消文字颜色
    private boolean enableCancel;   // 是否点框外关闭对话框
    private int width;              // 对话框的宽度
//    private DialogCallback callback;    // 按钮点击回调
    private RightBtnCallback rightCallback;    // 按钮点击回调
    private LeftBtnCallback leftCallback;    // 按钮点击回调
    private Dialog dialog;

    public DialogBuilder(Context mContext) {

        // 默认值设置
        titleTxtColor = Color.parseColor("#000000");
        contentTxtColor = Color.parseColor("#000000");
        rightTxtColor = Color.parseColor("#008BEF");
        leftTxtColor = Color.parseColor("#999999");

        width = 270;

        this.mContext = mContext;
    }

    /**
     * 实例化
     * @param mContext
     * @return
     */
    public static DialogBuilder from(Context mContext) {

        return new DialogBuilder(mContext);
    }

    /**
     * 设置标题
     * @param titleStr
     * @return
     */
    public DialogBuilder setTitle(String titleStr) {
        this.titleStr = titleStr;
        return this;
    }

    /**
     * 设置标题+颜色
     * @param titleStr
     * @return
     */
    public DialogBuilder setTitle(String titleStr, int txtColor) {
        this.titleStr = titleStr;
        this.titleTxtColor = txtColor;
        return this;
    }

    /**
     * 设置内容
     * @param contentStr
     * @return
     */
    public DialogBuilder setContent(String contentStr) {
        this.contentStr = contentStr;
        return this;
    }

    /**
     * 设置内容 + 颜色
     * @param contentStr
     * @return
     */
    public DialogBuilder setContent(String contentStr, int txtColor) {
        this.contentStr = contentStr;
        this.contentTxtColor = txtColor;
        return this;
    }

    /**
     * 设置确定按钮文字
     * @param rightTxt
     * @return
     */
    public DialogBuilder setRightBtn(String rightTxt) {
        this.rightTxt = rightTxt;
        return this;
    }

    /**
     * 设置确定按钮文字
     * @param rightTxt
     * @return
     */
    public DialogBuilder setRightBtn(String rightTxt, RightBtnCallback callback) {
        this.rightTxt = rightTxt;
        this.rightCallback = callback;
        return this;
    }

    /**
     * 设置确定按钮文字 + 颜色
     *
     * @param rightTxt
     * @param txtColor
     *          ContextCompat.getColor(context, colorId) 或者 Color.parseColor("#color")
     * @return
     */
    public DialogBuilder setRightBtn(String rightTxt, int txtColor, RightBtnCallback callback) {
        this.rightTxt = rightTxt;
        this.rightTxtColor = txtColor;
        this.rightCallback = callback;
        return this;
    }

    /**
     * 设置取消按钮文字
     * @param leftTxt
     * @return
     */
    public DialogBuilder setLeftBtn(String leftTxt) {
        this.leftTxt = leftTxt;
        return this;
    }

    /**
     * 设置取消按钮文字
     * @param leftTxt
     * @return
     */
    public DialogBuilder setLeftBtn(String leftTxt, LeftBtnCallback callback) {
        this.leftTxt = leftTxt;
        this.leftCallback = callback;
        return this;
    }

    /**
     * 设置取消按钮文字 + 颜色
     * @param leftTxt
     * @param txtColor
     *          ContextCompat.getColor(context, colorId) 或者 Color.parseColor("#color")
     * @return
     */
    public DialogBuilder setLeftBtn(String leftTxt, int txtColor, LeftBtnCallback callback) {
        this.leftTxt = leftTxt;
        this.leftTxtColor = txtColor;
        this.leftCallback = callback;
        return this;
    }

    /**
     * 是否点击窗口外关闭窗口
     * @param enableCancel
     * @return
     */
    public DialogBuilder setEnableCancel(boolean enableCancel) {
        this.enableCancel = enableCancel;
        return this;
    }

    /**
     * 窗口的宽度
     * @param width
     * @return
     */
    public DialogBuilder setWidth(int width){
        this.width = width;
        return this;
    }


    /**
     * 普通对话框
     */
    public DialogBuilder create() {
        try {

            AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.Theme_AppCompat_Dialog);
            View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_normal, null);
            builder.setView(view);
            dialog = builder.create();
            dialog.setCancelable(enableCancel);
            dialog.show();

            // ui处理
            LinearLayout titleLayout = view.findViewById(R.id.title_layout);
            if (TextUtils.isEmpty(titleStr)){
                titleLayout.setVisibility(View.GONE);
            }
            // 标题
            TextView titleText = view.findViewById(R.id.dg_title_text);
            titleText.setText(titleStr);
            titleText.setTextColor(titleTxtColor);
            // 内容
            TextView contentText = view.findViewById(R.id.dg_content_text);
            contentText.setText(contentStr);
            contentText.setTextColor(contentTxtColor);
            // 取消
            TextView leftBtn = view.findViewById(R.id.dg_left_btn);
            if (TextUtils.isEmpty(leftTxt)){
                leftBtn.setVisibility(View.GONE);
            }
            leftBtn.setText(leftTxt);
            leftBtn.setTextColor(leftTxtColor);
            leftBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    if (null != leftCallback) {
                        leftCallback.onLeftPressed();
                    }
                }
            });
            // 确定
            TextView rightText = view.findViewById(R.id.dg_right_btn);
            if (TextUtils.isEmpty(rightTxt)){
                rightText.setVisibility(View.GONE);
            }
            rightText.setText(rightTxt);
            rightText.setTextColor(rightTxtColor);
            rightText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    if (null != rightCallback) {
                        rightCallback.onRightPressed();
                    }
                }
            });

            // 限制宽度
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = DimensUtils.dip2px(mContext, width);
            dialog.getWindow().setBackgroundDrawableResource(R.drawable.shape_white_bg_10);
            dialog.getWindow().setAttributes(lp);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return this;
    }


    public Dialog getDialog() {
        return dialog;
    }

    public interface RightBtnCallback {
        void onRightPressed();
    }

    public interface LeftBtnCallback {
        void onLeftPressed();
    }

}