自定义dialog

basedialog

public abstract class BaseDialog extends Dialog {
    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public BaseDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated methodstub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (KeyboardHelper.isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少,否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

}

base基类


abstract class BaseCommonTipsDialog<E>: BaseDialog {
    var activity: Activity? = null
    constructor(context: Context) : super(context)
    constructor(context: Context, @LayoutRes  resource: Int, data: E): super(context){
        activity = if (context is Activity) context else null
        val isInstanceCancellation = data is BaseAlertDialog.Cancellation
        if (isInstanceCancellation) {
            val cancellation = data as Cancellation
            setCancelable(cancellation.getCancelable())
            setCanceledOnTouchOutside(cancellation.getCancelOutside())
        }
        window!!.requestFeature(Window.FEATURE_NO_TITLE)
        window!!.setBackgroundDrawableResource(R.drawable.transparent)
        val view: View = LayoutInflater.from(context).inflate(resource, null)
        setContentView(view)
        setup(view,data)
    }

    open class Cancellation {
        private var cancelOutside = true
        private var cancelable = true
        fun getCancelable(): Boolean{
            return cancelable
        }
        fun getCancelOutside(): Boolean{
            return cancelOutside
        }
        protected fun setCancelOutsideValue(cancelOutside: Boolean) {
            this.cancelOutside = cancelOutside
        }

        protected fun setCancelableValue(cancelable: Boolean) {
            this.cancelable = cancelable
        }

    }
    protected abstract fun setup(dialogView: View, data: E)
    open fun showDialog() {
        show()
    }

    open fun dismissDialog() {
        if (!activity?.isFinishing!! && isShowing) {
            dismiss()
        }
    }
}

子类


class CommonTipsDialog(context: Context, resource: Int, builder: Builder) :
    BaseCommonTipsDialog<CommonTipsDialog.Builder>(context, resource, builder) {

    override fun setup(dialogView: View, data: Builder) {
        val blurView: BlurView = findViewById(R.id.bg_blurView)
        val cl: ConstraintLayout = findViewById(R.id.cl_bg)
        val clDialog: ConstraintLayout = findViewById(R.id.cl_dialog_bg)
        val imageView: ImageView = findViewById(R.id.img_tip)
        val tvContent: TextView = findViewById(R.id.tv_tips)
        val tvTitle: TextView = findViewById(R.id.tv_title)
        val buttonTop: Button = findViewById(R.id.bt_top)
        val buttonBottom: Button = findViewById(R.id.bt_bottom)




        val wm = scanForActivity(context)?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        wm.defaultDisplay.getMetrics(dm)
        val width = dm.widthPixels
        // 屏幕宽度(像素)
        val height = dm.heightPixels
        // 屏幕高度(像素)
        val density = dm.density
        //屏幕密度(0.75 / 1.0 / 1.5)
        val densityDpi = dm.densityDpi
        //屏幕密度dpi(120 / 160 / 240)
        val layoutParams: WindowManager.LayoutParams = window?.attributes!!
        //这个地方可以用ViewGroup.LayoutParams.MATCH_PARENT属性,各位试试看看有没有效果
        layoutParams.width = width
        layoutParams.height = height
        window?.attributes = layoutParams

        val decorView: View = scanForActivity(context)?.window?.decorView!!
        val rootView: ViewGroup = decorView.findViewById(android.R.id.content)
        val windowBackground = decorView.background


        blurView.setupWith(rootView, RenderScriptBlur(context))
            .setFrameClearDrawable(windowBackground)
            .setBlurRadius(3f)

        data.getTips()?.let {  tvContent.text = it}

        data.getTitle()?.let { tvTitle.text = it }

        data.getBtnTextTop()?.let {  buttonTop.text = it}

        data.getBtnTextBottom()?.let { buttonBottom.text = it }

        data.getImgId()?.let {   imageView.setImageDrawable(context.getDrawable(it))}
        data.getColor()?.let {
            tvTitle.setTextColor(it)
            tvContent.setTextColor(it)
        }

        data.getTopListener()?.let {  buttonTop.setOnClickListener(it)}

        data.getBottomListener()?.let { buttonBottom.setOnClickListener(it) }

        data.getIsNeedTipsBold()?.let { if (it){ tvContent.setTypeface(null, Typeface.BOLD) } }
        data.getOutsideCancelable()?.let {
            if (it){
                cl.setOnClickListener{
                    dismissDialog()
                }
                clDialog.setOnClickListener{
                }
            }
        }
        data.getIsShowBtnTop()?.let {
            if (it){
                buttonTop.visibility = View.VISIBLE
            }else{
                buttonTop.visibility = View.GONE
            }
        }
        data.getIsShowTitle()?.let {
            if (it){
                tvTitle.visibility = View.VISIBLE
                val lp = tvContent.layoutParams as ConstraintLayout.LayoutParams
                //方式一: 设置边距 且控制大小
                lp.setMargins(ScreenUtil.dp2px(40f), ScreenUtil.dp2px(15f), ScreenUtil.dp2px(40f),0)
                tvContent.layoutParams = lp
            }else{
                tvTitle.visibility = View.GONE
            }
        }
        data.getIsShowBtnBottom()?.let {
            if (it){
                buttonBottom.visibility = View.VISIBLE
            }else{
                buttonBottom.visibility = View.GONE
                val lp = buttonTop.layoutParams as ConstraintLayout.LayoutParams
                //方式一: 设置边距 且控制大小
                lp.setMargins(0, ScreenUtil.dp2px(20f), 0, ScreenUtil.dp2px(30f))
                buttonTop.layoutParams = lp
            }
        }
        data.getIsSetLongMargin()?.let {
            if (it){
                val lp = buttonTop.layoutParams as ConstraintLayout.LayoutParams
                //方式一: 设置边距 且控制大小
                lp.setMargins(0, ScreenUtil.dp2px(40f), 0,ScreenUtil.dp2px(50f))
                buttonTop.layoutParams = lp
            }
        }
    }

    private fun scanForActivity(cont: Context?): Activity? {
        if (cont == null) return null else if (cont is Activity) return cont else if (cont is ContextWrapper) return scanForActivity(
            cont.baseContext
        )
        return null
    }

    class Builder(private val context: Context): Cancellation(){
        private var tips: String? = null
        private var title: String? = null
        private var isNeedTipsBold = false
        private var isShowTitle = false
        private var isShowBtnTop = true
        private var isShowBtnBottom = false
        private var isSetLongMargin = false
        private var btnTextTop: String? = null
        private var btnTextBottom: String? = null
        private var imgId: Int? = null
        private var color: Int? = null
        private var listenerTop: View.OnClickListener? = null
        private var listenerBottom: View.OnClickListener? = null
        fun getIsSetLongMargin(): Boolean?{
            return this.isSetLongMargin
        }
        fun getTips(): String? {
            return this.tips
        }
        fun getTitle(): String? {
            return this.title
        }
        fun getBtnTextBottom(): String?{
            return this.btnTextBottom
        }
        fun getBtnTextTop(): String?{
            return this.btnTextTop
        }
        fun getTopListener(): View.OnClickListener?{
            return this.listenerTop
        }
        fun getBottomListener(): View.OnClickListener?{
            return this.listenerBottom
        }
        fun getImgId(): Int?{
            return this.imgId
        }
        fun getColor(): Int?{
            return this.color
        }
        fun getIsNeedTipsBold(): Boolean?{
            return this.isNeedTipsBold
        }
        fun getIsShowTitle(): Boolean?{
            return this.isShowTitle
        }
        fun getIsShowBtnBottom(): Boolean?{
            return this.isShowBtnBottom
        }
        fun getIsShowBtnTop(): Boolean?{
            return this.isShowBtnTop
        }
        fun getOutsideCancelable(): Boolean?{
            return getCancelOutside()
        }
        fun setIsNeedTipsBold(isNeedTipsBold: Boolean): Builder {
            this.isNeedTipsBold = isNeedTipsBold
            return this
        }
        fun setIsShowTitle(isShowTitle: Boolean): Builder {
            this.isShowTitle = isShowTitle
            return this
        }
        fun setIsSetLongMargin(isSetLongMargin: Boolean): Builder {
            this.isSetLongMargin = isSetLongMargin
            return this
        }
        fun setIsShowBtnTop(isShowBtnTop: Boolean): Builder {
            this.isShowBtnTop = isShowBtnTop
            return this
        }
        fun setIsShowBottom(isShowBtnBottom: Boolean): Builder {
            this.isShowBtnBottom = isShowBtnBottom
            return this
        }
        fun setTips(tips: String): Builder {
            this.tips = tips
            return this
        }
        fun setBtnTextTop(btnText: String): Builder {
            this.btnTextTop = btnText
            isShowBtnTop = true
            return this
        }
        fun setBtnTextBottom(btnText: String): Builder {
            this.btnTextBottom = btnText
            isShowBtnBottom = true
            return this
        }
        fun setImgId(imgId: Int): Builder {
            this.imgId = imgId
            return this
        }
        fun setColor(color: Int): Builder {
            this.color = color
            return this
        }
        fun setTitle(title: String): Builder {
            this.title = title
            isShowTitle = true
            return this
        }
        fun setTopListener(listener: View.OnClickListener): Builder {
            this.listenerTop = listener
            return this
        }
        fun setBottomListener(listener: View.OnClickListener): Builder {
            this.listenerBottom = listener
            return this
        }
        fun setCancelable(cancelable: Boolean): Builder {
            setCancelableValue(cancelable)
            return this
        }
        fun setOutsideCancelable(cancelable: Boolean): Builder {
            setCancelOutsideValue(cancelable)
            return this
        }
        fun build(): CommonTipsDialog {
            return CommonTipsDialog(context, R.layout.dialog_img_tips, this)
        }
    }

    //使用示例
//    var imageTipsDialog: ImageTipsDialog? = null
//    imageTipsDialog = ImageTipsDialog.Builder(this)
//    .setTips("superaste el límite de contraseñas\ncreadas por día.\nInténtalo dentro de 24 horas.")
//    .setTopListener() {
//        imageTipsDialog?.dismissDialog()
//    }
//    .setOutsideCancelable(false)
//    .setTitle("455")
//    .setBtnTextBottom("555")
//    .setColor(Color.parseColor("#404040"))
//    .setImgId(R.drawable.image10)
//    .build()
        setDialogMatchParent(imageTipsDialog!!)
//    imageTipsDialog?.showDialog()

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值