怎么在kotlin中利用建造者模式自定義一個對話框?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站制作、網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的長樂網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
1.CommonDialog 創(chuàng)建我們自己的對話框,繼承于系統(tǒng)的Dialog 實現(xiàn)構造方法
class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {}
2. 在內部創(chuàng)建BUilder類 定義出我們需要的方法和屬性
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以外的區(qū)域時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 } }
3.在需要的地方使用
CommonDialog.Builder(this). setImageHeader(R.mipmap.icon_gantan_tankuang) .setTitle("你是否要注銷賬戶") .setMessage("注銷后需重新注冊才能使用牛返返優(yōu)惠") .setPositiveButton("確定注銷", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() DestroyAccount() }) .setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() }) .setWith(0.77f) .create() .show()
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享文章:怎么在kotlin中利用建造者模式自定義一個對話框
本文URL:http://muchs.cn/article0/jsojoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站導航、App設計、建站公司、域名注冊
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)