AlertDialog.java 8.3 KB
/*
 * Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
 */

package com.wd.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkitcore.tools.ResUtils;


/**
 * @author yangchenggong
 * @date 2022/6/28 15:29
 */
public class AlertDialog {
    private Context context;

    private Dialog dialog;

    private LinearLayout lLayout_bg;

    private TextView txt_title;

    private TextView txt_msg;

    private TextView btn_neg;

    private TextView btn_pos;

    private ImageView img_line;

    private ImageView horizontal_line;

    private Display display;

    private boolean showTitle = false;

    private boolean showMsg = false;

    private boolean showPosBtn = false;

    private boolean showNegBtn = false;

    public AlertDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public AlertDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);

        // 获取自定义Dialog布局中的控件
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_title.setVisibility(View.GONE);
        txt_msg = (TextView) view.findViewById(R.id.txt_msg);
        txt_msg.setVisibility(View.GONE);
        btn_neg = view.findViewById(R.id.btn_neg);
        btn_neg.setVisibility(View.GONE);
        btn_pos = view.findViewById(R.id.btn_pos);
        btn_pos.setVisibility(View.GONE);
        horizontal_line = view.findViewById(R.id.horizontal_line);
        img_line = (ImageView) view.findViewById(R.id.img_line);
        img_line.setVisibility(View.GONE);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.dimAmount = 0.3f;
        // lp.y = 250;
        dialog.getWindow().setAttributes(lp);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(
            new FrameLayout.LayoutParams((int) (display.getWidth() * 0.75), LayoutParams.WRAP_CONTENT));
        //设置按压效果
        setButtonSelect();
        return this;
    }

    public AlertDialog setNightKeep() {
        if(lLayout_bg != null){
            lLayout_bg.setBackground(ContextCompat.getDrawable(context,R.drawable.alert_bg_keep));
            txt_title.setTextColor(ContextCompat.getColor(context,R.color.res_color_general_333333_keep));
            txt_msg.setTextColor(ContextCompat.getColor(context,R.color.res_color_common_C3_keep));
            horizontal_line.setBackground(ContextCompat.getDrawable(context,R.color.res_color_common_C7_keep));
            img_line.setBackground(ContextCompat.getDrawable(context,R.color.res_color_common_C7_keep));
            btn_pos.setTextColor(ContextCompat.getColor(context,R.color.res_color_general_333333_keep));
        }
        return this;
    }

    public AlertDialog setTitle(String title) {
        showTitle = true;
        txt_title.setText(TextUtils.isEmpty(title)?"Title":title);
        return this;
    }

    public AlertDialog setMsg(String msg) {
        showMsg = true;
        txt_msg.setText(TextUtils.isEmpty(msg)?"Msg":msg);
        return this;
    }

    public AlertDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public AlertDialog setPositiveButton(String text, final OnClickListener listener) {
        showPosBtn = true;
        btn_pos.setText(TextUtils.isEmpty(text)? ResUtils.getString(R.string.yes_btn) :text);
        btn_pos.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if(listener != null){
                    listener.onClick(v);
                }
            }
        });
        return this;
    }

    public AlertDialog setNegativeButton(String text, final OnClickListener listener) {
        showNegBtn = true;
        btn_neg.setText(TextUtils.isEmpty(text)?ResUtils.getString(R.string.res_cancel) :text);
        btn_neg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

    private void setLayout() {
        if (!showTitle && !showMsg) {
            txt_title.setText(ResUtils.getString(R.string.tips));
            txt_title.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            txt_title.setVisibility(View.VISIBLE);
        }

        if (showMsg) {
            txt_msg.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && !showNegBtn) {
            btn_pos.setText(ResUtils.getString(R.string.yes_btn));
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

        if (showPosBtn && showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
            btn_neg.setVisibility(View.VISIBLE);
            img_line.setVisibility(View.VISIBLE);
        }

        if (showPosBtn && !showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && showNegBtn) {
            btn_neg.setVisibility(View.VISIBLE);
        }
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            setLayout();
            dialog.show();
        }
    }

    /**
     * 设置变粗
     */
    private void setButtonSelect(){
        btn_pos.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    // When the user clicks the Button
                    case MotionEvent.ACTION_DOWN:
                        btn_pos.setTypeface(Typeface.DEFAULT_BOLD);
                        break;

                    // When the user releases the Button
                    case MotionEvent.ACTION_UP:
                        btn_pos.setTypeface(Typeface.DEFAULT);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        btn_neg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    // When the user clicks the Button
                    case MotionEvent.ACTION_DOWN:
                        btn_neg.setTypeface(Typeface.DEFAULT_BOLD);
                        break;

                    // When the user releases the Button
                    case MotionEvent.ACTION_UP:
                        btn_neg.setTypeface(Typeface.DEFAULT);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }

    /**
     * 是否在展示
     * @return
     */
    public boolean isShow(){
        if(dialog != null){
            return dialog.isShowing();
        }
        return false;
    }

    /**
     * 隐藏弹框
     */
    public void dismissDialog(){
        if(dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }
}