kotlin使用建造者模式自定義對(duì)話框
本文實(shí)例為大家分享了kotlin自定義對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
1.CommonDialog 創(chuàng)建我們自己的對(duì)話框,繼承于系統(tǒng)的Dialog 實(shí)現(xiàn)構(gòu)造方法
class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {}
2. 在內(nèi)部創(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的布局頁(yè)面 */ 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(), "未設(shè)置對(duì)話框標(biāo)題!"); } 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) /** * 禁止點(diǎn)擊Dialog以外的區(qū)域時(shí)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("注銷后需重新注冊(cè)才能使用牛返返優(yōu)惠") .setPositiveButton("確定注銷", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() DestroyAccount() }) .setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() }) .setWith(0.77f) .create() .show()
實(shí)現(xiàn)效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- android 對(duì)話框彈出位置和透明度的設(shè)置具體實(shí)現(xiàn)方法
- Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
- Android使用setCustomTitle()方法自定義對(duì)話框標(biāo)題
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)
- Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
相關(guān)文章
Android 利用ViewPager+GridView實(shí)現(xiàn)首頁(yè)導(dǎo)航欄布局分頁(yè)效果
用ViewPager+GridView實(shí)現(xiàn)首頁(yè)導(dǎo)航欄布局分頁(yè)效果來(lái)實(shí)現(xiàn)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android開(kāi)發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊
這篇文章主要給大家介紹了關(guān)于Android開(kāi)發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺(tái)訂單支付API設(shè)計(jì))
這篇文章主要介紹了Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺(tái)訂單支付API設(shè)計(jì))的相關(guān)資料,需要的朋友可以參考下2016-11-11Android模仿實(shí)現(xiàn)微博詳情頁(yè)滑動(dòng)固定頂部欄的效果實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Android模仿實(shí)現(xiàn)微博詳情頁(yè)滑動(dòng)固定頂部欄效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果的方法,涉及Android界面布局,事件響應(yīng)及Bitmap操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android編程實(shí)現(xiàn)的一鍵鎖屏程序詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)的一鍵鎖屏程序,結(jié)合實(shí)例形式詳細(xì)分析了Android一鍵鎖屏的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10