第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Kotlin用建造者模式創(chuàng)建對(duì)話框

標(biāo)簽:
Android
class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {

    /**
     * 建造器的构造方法:
     *
     * @param context
     */

    class Builder (private val context: Context) {
        private var title: String? = null
        private var message: String? = null
        private var positiveButtonContent: String? = null
        private var negativeButtonContent: String? = null
        private var positiveButtonListener: DialogInterface.OnClickListener? = null
        private var negativeButtonListener: DialogInterface.OnClickListener? = null
        private var contentView: View? = null
        private var imageid: Int = 0
        private var color: Int = 0
        private var withOffSize: Float = 0.toFloat()
        private var heightOffSize: Float = 0.toFloat()


        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }


        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        fun setMessageColor(color: Int): Builder {
            this.color = color
            return this
        }

        fun setImageHeader(Imageid: Int): Builder {

            this.imageid = Imageid
            return this
        }


        fun setPositiveButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = text
            this.positiveButtonListener = listener
            return this
        }

        fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = context.getText(textId) as String
            this.positiveButtonListener = listener
            return this
        }

        fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonContent = text
            this.negativeButtonListener = listener
            return this
        }

        fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonContent = context.getText(textId) as String
            this.negativeButtonListener = listener
            return this
        }

        fun setContentView(v: View): Builder {
            this.contentView = v
            return this
        }

        fun setWith(v: Float): Builder {
            this.withOffSize = v
            return this
        }

        fun setContentView(v: Float): Builder {
            this.heightOffSize = v
            return this
        }

        fun create(): CommonDialog {
            /**
             * 利用我们刚才自定义的样式初始化Dialog
             */
            val dialog = CommonDialog(context,
                    R.style.dialogStyle)
            /**
             * 下面就初始化Dialog的布局页面
             */
            val inflater = context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val dialogLayoutView = inflater.inflate(R.layout.dialog_layout,
                    null)
            dialog.addContentView(dialogLayoutView, ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))

            if (imageid != 0) {
                (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView)
                        .setImageResource(imageid)
            } else {
                (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(title)) {
                (dialogLayoutView.findViewById<View>(R.id.tv_dialog_title) as TextView).text = title
            } else {
                // Log.w(context.getClass().toString(), "未设置对话框标题!");
            }

            if (color != 0) {
                val viewById = dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView
                viewById.setTextColor(color)
            }

            if (!TextUtils.isEmpty(message)) {
                (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).text = message
            } else if (contentView != null) {
                (dialogLayoutView
                        .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout)
                        .removeAllViews()
                (dialogLayoutView
                        .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout).addView(
                        contentView, ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT))
            } else {
                (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).visibility = View.INVISIBLE
            }

            if (!TextUtils.isEmpty(positiveButtonContent)) {
                (dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).text = positiveButtonContent
                if (positiveButtonListener != null) {
                    (dialog.findViewById<View>(R.id.tv_dialog_pos) as TextView)
                            .setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }

                }
            } else {
                (dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).visibility = View.GONE
                dialogLayoutView.findViewById<View>(R.id.line).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(negativeButtonContent)) {
                (dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).text = negativeButtonContent
                if (negativeButtonListener != null) {
                    (dialogLayoutView
                            .findViewById<View>(R.id.tv_dialog_neg) as TextView)
                            .setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }
                }
            } else {
                (dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).visibility = View.GONE
            }
            /**
             * 将初始化完整的布局添加到dialog中
             */
            dialog.setContentView(dialogLayoutView)
            /**
             * 禁止点击Dialog以外的区域时Dialog消失
             */
            dialog.setCanceledOnTouchOutside(false)


            val window = dialog.window
            val context = this.context as Activity
            val windowManager = context.windowManager

            val defaultDisplay = windowManager.defaultDisplay

            val attributes = window!!.attributes

            if (withOffSize.toDouble() != 0.0) {

                attributes.width = (defaultDisplay.width * withOffSize).toInt()
            } else {
                attributes.width = (defaultDisplay.width * 0.77).toInt()

            }
            if (heightOffSize.toDouble() != 0.0) {

                attributes.height = (defaultDisplay.height * heightOffSize).toInt()
            }
            window.attributes = attributes
            return dialog
        }
    }

}

使用:

        CommonDialog.Builder(this).
                setImageHeader(R.mipmap.icon_gantan_tankuang)
                .setTitle("你是否要注销账户")
                .setMessage("注销后需重新注册才能使用牛返返优惠")
                .setPositiveButton("确定注销", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    DestroyAccount()
                })
                .setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create()
                .show()

效果图:
图片描述

补充 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_200"
    android:background="@drawable/jiangli"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/dialog_llyout_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_image_header"
        android:layout_marginTop="@dimen/dp_20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/icon_gantan_tankuang" />

    <TextView
        android:id="@+id/tv_dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_8"
        android:gravity="center"
        android:textColor="@color/title_color"
        android:textSize="@dimen/sp_14" />

    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_12"
        android:layout_marginBottom="@dimen/dp_12"
        android:gravity="center"
        android:text="注销后需重新注册才能使用牛返返优惠"
        android:textColor="@color/text_color_666666"
        android:textSize="@dimen/sp_14" />
</LinearLayout>


    <include layout="@layout/line" />
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <TextView

        android:id="@+id/tv_dialog_pos"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="@dimen/dp_50"
        android:gravity="center"
        android:text="确定注销"
        android:textColor="@color/title_color"
        android:textSize="@dimen/sp_16" />
<View
    android:id="@+id/line"
    android:layout_width="@dimen/dp_0_5"
    android:layout_height="match_parent"

    android:background="@color/colorLine"
    />
    <TextView

        android:id="@+id/tv_dialog_neg"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="@dimen/dp_50"
        android:gravity="center"
        android:text="取消"
        android:textColor="#ff9502"
        android:textSize="@dimen/sp_16" />

</LinearLayout>


</LinearLayout>

點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消