麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 開發(fā) > 綜合 > 正文

kotlin使用建造者模式自定義對(duì)話框

2024-07-21 23:03:48
字體:
供稿:網(wǎng)友

本文實(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)效果:

kotlin,建造者模式,對(duì)話框

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到kotlin教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久亚洲精品国产一区 | 99国产精品欲a | 欧日韩在线视频 | 国产1区2区3区中文字幕 | 久久网站热最新地址 | 黄色特级视频 | 欧美1区2区在线观看 | 国产一区二区在线免费观看 | 国产a一级片 | 国产一级淫片免费看 | chinese军人gay呻吟 | 亚洲网站在线观看 | 成人午夜免费观看 | 日韩欧美电影一区二区三区 | 大学生一级毛片在线视频 | 黄色片网站免费在线观看 | 久久精品亚洲一区二区 | 欧美亚洲国产成人 | 色羞羞| 久久吊| 少妇色诱麻豆色哟哟 | 一级成人毛片 | 国产精品成人av片免费看最爱 | 全黄性性激高免费视频 | 国产一级免费在线视频 | www.精品一区 | 一级成人欧美一区在线观看 | 天天透天天狠天天爱综合97 | 性少妇videosexfreexxx片 | av国产免费 | 欧美77 | 一区在线不卡 | 欧美一级黄视频 | 在线播放黄色片 | 久久久久久久免费看 | 久久久久91视频 | 视频在线中文字幕 | 久久久久久久不卡 | 久久久涩 | 视频一区 日韩 | 成人在线视频免费播放 |