Android开发之Dialog对话框(弹框)工具类

公司项目里面扣出来的

我们来看下弹框效果

Android开发之Dialog对话框(弹框)工具类_第1张图片

直接上代码

ScreenSizeUtils.java
package 导入自己的包;

import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;

/**
 * 获取屏幕尺寸信息
 *
 */
public class ScreenSizeUtils {

    public static Point getScreenSizeInfo(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = null;
        if (manager != null) {
            display = manager.getDefaultDisplay();
            Point sizePoint = new Point();
            display.getSize(sizePoint);
            return sizePoint;
        } else {
            return null;
        }
    }

}
LoadingDialog.java
package 导入自己报名即可;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import 导入自己报名.ScreenSizeUtils;

/**
 * 加载进度条Dialog
 *
 * @author on 2018/9/17.
 */
public class LoadingDialog extends Dialog {

    private LoadingDialog(Context context) {
        super(context, R.style.loading_dialog_style);
    }

    public static class Builder {

        private Context mContext;
        private CharSequence mMessage;
        private View mContentView;
        private float mWidthPercent = 0.3f;
        private float mHeightPercent = 0.3f;
        private boolean mIsCancelable;
        private boolean mIsOutsideCancelable;
        private DialogInterface.OnCancelListener mCancelListener;
        private DialogInterface.OnDismissListener mDismissListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setMessage(CharSequence message) {
            mMessage = message;
            return this;
        }

        public Builder setContentView(View view) {
            mContentView = view;
            return this;
        }

        /**
         * 设置dialog宽
         *
         * @param widthPercent 占屏幕宽度百分比 0 - 1
         * @return builder
         */
        public Builder setWidthPercent(float widthPercent) {
            mWidthPercent = widthPercent;
            return this;
        }

        /**
         * 设置dialog高
         *
         * @param heightPercent 占屏幕高度百分比 0 - 1
         * @return builder
         */
        public Builder setHeightPercent(float heightPercent) {
            mHeightPercent = heightPercent;
            return this;
        }

        public Builder setLoadingCancelable(boolean cancelable) {
            mIsCancelable = cancelable;
            return this;
        }

        public Builder setLoadingOutsideCancelable(boolean cancelable) {
            mIsOutsideCancelable = cancelable;
            return this;
        }

        public Builder setListener(DialogInterface.OnCancelListener cancelListener,
                                   DialogInterface.OnDismissListener dismissListener) {
            mCancelListener = cancelListener;
            mDismissListener = dismissListener;
            return this;
        }

        public LoadingDialog create() {
            LoadingDialog loadingDialog = new LoadingDialog(mContext);
            loadingDialog.setCancelable(mIsCancelable);
            loadingDialog.setCanceledOnTouchOutside(mIsOutsideCancelable);
            if (null != mCancelListener) {
                loadingDialog.setOnCancelListener(mCancelListener);
            }
            if (null != mDismissListener) {
                loadingDialog.setOnDismissListener(mDismissListener);
            }
            if (null != mContentView) {
                loadingDialog.setContentView(mContentView);
            } else {
                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_loading, null);
                ProgressBar loadingPB = (ProgressBar) view.findViewById(R.id.progress_loading);
                TextView messageTV = (TextView) view.findViewById(R.id.tv_loading_message);
                loadingPB.setIndeterminate(false);
                messageTV.setText(TextUtils.isEmpty(mMessage) ? "加载中..." : mMessage);
                loadingDialog.setContentView(view);
            }
            loadingDialog.show();
            setWindow(loadingDialog);
            return loadingDialog;
        }

        private void setWindow(LoadingDialog dialog) {
            //获取对话框当前的参数值
            Window window = dialog.getWindow();
            if (null != window) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                //宽高可设置具体大小
                Point point = ScreenSizeUtils.getScreenSizeInfo(mContext);
                if (null != point) {
                    lp.width = (int) ((point.x) * mWidthPercent);
                    lp.height = (int) ((point.x) * mHeightPercent);
                }
                window.setAttributes(lp);
            }
        }
    }

}
LoadingDialogUtils.java
package 导入自己的包;

import android.content.Context;
import android.content.DialogInterface;

import 导入自己的包.LoadingDialog;

/**
 * 加载进度条Dialog工具类
 *
 * @author xlg on 2018/9/17.
 */
public final class LoadingDialogUtils {

    private static LoadingDialog mLoadingDialog;

    /**
     * 显示进度条Dialog
     *
     * @param context 上下文
     */
    public static void showLoadingDialog(Context context) {
        show(context, null, true, null, null);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context 上下文
     * @param msg     loading显示的message
     */
    public static void showLoadingDialog(Context context, CharSequence msg) {
        show(context, msg, true, null, null);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context 上下文
     */
    public static void showLoadingDialog(Context context, boolean isCCancel) {
        show(context, null, isCCancel, null, null);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context 上下文
     * @param msg     loading显示的message
     */
    public static void showLoadingDialog(Context context, CharSequence msg, boolean isCCancel) {
        show(context, msg, isCCancel, null, null);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context         上下文
     * @param cancelListener  取消Dialog显示监听
     * @param dismissListener Dialog消失监听
     */
    public static void showLoadingDialog(Context context,
                                         DialogInterface.OnCancelListener cancelListener,
                                         DialogInterface.OnDismissListener dismissListener) {
        show(context, null, true, cancelListener, dismissListener);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context         上下文
     * @param cancelListener  取消Dialog显示监听
     * @param dismissListener Dialog消失监听
     */
    public static void showLoadingDialog(Context context, CharSequence msg,
                                         DialogInterface.OnCancelListener cancelListener,
                                         DialogInterface.OnDismissListener dismissListener) {
        show(context, msg, true, cancelListener, dismissListener);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context         上下文
     * @param cancelListener  取消Dialog显示监听
     * @param dismissListener Dialog消失监听
     */
    public static void showLoadingDialog(Context context, CharSequence msg, boolean isCCancel,
                                         DialogInterface.OnCancelListener cancelListener,
                                         DialogInterface.OnDismissListener dismissListener) {
        show(context, msg, isCCancel, cancelListener, dismissListener);
    }

    /**
     * 显示进度条Dialog
     *
     * @param context         上下文
     * @param isCCancel       是否点击Dialog边缘取消Dialog
     * @param cancelListener  取消Dialog显示监听
     * @param dismissListener Dialog消失监听
     */
    private static void show(Context context, CharSequence message, boolean isCCancel,
                             DialogInterface.OnCancelListener cancelListener,
                             DialogInterface.OnDismissListener dismissListener) {
        mLoadingDialog = new LoadingDialog.Builder(context)
                .setMessage(message)
                .setLoadingCancelable(isCCancel)
                .setLoadingOutsideCancelable(isCCancel)
                .setListener(cancelListener, dismissListener)
                .create();
    }

    /**
     * 隐藏进度条Dialog
     * 由于统一管理Dialog,注意Activity/Fragment生命周期对它的影响
     */
    public static void hideLoadingDialog() {
        if (null != mLoadingDialog) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
            mLoadingDialog = null;
        }
    }

}

 

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