ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

封装dialog

2021-04-26 16:57:32  阅读:189  来源: 互联网

标签:return int Builder AlertDialog dialog 封装 public


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有