其他分享
首页 > 其他分享> > 封装dialog

封装dialog

作者:互联网

public class AlertDialog extends Dialog {
    AlertController mAlert;

    public AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mAlert = new AlertController(this, getWindow());
    }


    public void setText(int viewId, CharSequence text) {
        mAlert.setText(viewId, text);
    }

    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mAlert.setOnClickListener(viewId, listener);
    }


    /**
     * 减少findviewById的次数
     */
    public <T extends View> T getView(int viewId) {

        return mAlert.getView(viewId);
    }

    public static class Builder {

        private final AlertController.AlertParams P;

        public Builder(@NonNull Context context) {
            this(context, R.style.dialog);

        }

        public Builder(@NonNull Context context, @StyleRes int themeResId) {
            P = new AlertController.AlertParams(context, themeResId);
        }

        public AlertDialog create() {
            final AlertDialog dialog = new AlertDialog(P.mContext, P.themeResId);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable());
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        /**
         * 设置View
         */
        public AlertDialog.Builder setContentView(int layoutResId) {
            P.mView = null;
            P.mViewLayoutResId = layoutResId;
            return this;
        }

        public AlertDialog.Builder setContentView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }

        /**
         * 存放自定义View中的文本
         */
        public AlertDialog.Builder setText(int layoutId, CharSequence text) {
            P.mTextArray.put(layoutId, text);
            return this;
        }

        /**
         * 存放自定义View中按钮点击事件
         */
        public AlertDialog.Builder setOnClickListener(int layoutId, View.OnClickListener listener) {
            P.mClickArray.put(layoutId, listener);
            return this;
        }


        //配置一些万能参数
        public AlertDialog.Builder fullWidth() {
            P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        //设置动画
        public AlertDialog.Builder fromButtom(boolean isAnimation) {
            if (isAnimation) {
                P.mAnimation = R.style.dialog_from_bottom_anim;
            }
            P.mGravity = Gravity.BOTTOM;
            return this;
        }

        public AlertDialog.Builder setWidthAndHeight(int width, int heigth) {
            P.mWidth = width;
            P.mHeigth = heigth;
            return this;
        }

        //设置默认动画
        public AlertDialog.Builder AddDefaultAnimation() {
            P.mAnimation = R.style.dialog_scale_anim;
            return this;
        }

        //设置动画
        public AlertDialog.Builder setAnimation(int styleAnimation) {
            P.mAnimation = styleAnimation;
            return this;
        }

        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}

 

标签:return,int,Builder,AlertDialog,dialog,封装,public
来源: https://blog.csdn.net/qq_36310162/article/details/116161921