最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

kotlin使用建造者模式自定義對話框

 更新時(shí)間:2018年07月27日 11:52:58   作者:一直學(xué)習(xí)中的小  
這篇文章主要為大家詳細(xì)介紹了kotlin使用建造者模式自定義對話框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了kotlin自定義對話框的具體代碼,供大家參考,具體內(nèi)容如下

1.CommonDialog 創(chuàng)建我們自己的對話框,繼承于系統(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的布局頁面
       */
      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è)置對話框標(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("注銷后需重新注冊才能使用牛返返優(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)效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android獲取實(shí)時(shí)連接熱點(diǎn)的設(shè)備IP

    Android獲取實(shí)時(shí)連接熱點(diǎn)的設(shè)備IP

    這篇文章主要介紹了Android獲取實(shí)時(shí)連接熱點(diǎn)的設(shè)備IP 的相關(guān)資料,文中給大家補(bǔ)充介紹了安卓獲取接入的Wifi熱點(diǎn)設(shè)備的Ip地址的代碼,需要的朋友可以參考下
    2018-01-01
  • Android DrawLayout結(jié)合ListView用法實(shí)例

    Android DrawLayout結(jié)合ListView用法實(shí)例

    這篇文章主要介紹了Android DrawLayout結(jié)合ListView用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Android 中SQLite技術(shù)實(shí)例詳解

    Android 中SQLite技術(shù)實(shí)例詳解

    這篇文章主要介紹了Android 中SQLite技術(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android開發(fā)使用HttpURLConnection進(jìn)行網(wǎng)絡(luò)編程詳解【附源碼下載】

    Android開發(fā)使用HttpURLConnection進(jìn)行網(wǎng)絡(luò)編程詳解【附源碼下載】

    這篇文章主要介紹了Android開發(fā)使用HttpURLConnection進(jìn)行網(wǎng)絡(luò)編程的方法,結(jié)合實(shí)例形式分析了Android基于HttpURLConnection實(shí)現(xiàn)顯示圖片與文本功能,涉及Android布局、文本解析、數(shù)據(jù)傳輸、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android入門之ScrollView的使用教程

    Android入門之ScrollView的使用教程

    我們經(jīng)??梢钥吹皆谑謾C(jī)里正在垂直加載一堆的數(shù)據(jù),然后過一會加載得內(nèi)容過多,到了手機(jī)的底部了,垂直方向就會出現(xiàn)一個(gè)“滾動(dòng)條”。本文就來通過一些示例和大家介紹下ScrollView(滾動(dòng)條)的使用,感興趣的可以了解一下
    2022-11-11
  • Android編程解析Json格式數(shù)據(jù)的方法

    Android編程解析Json格式數(shù)據(jù)的方法

    這篇文章主要介紹了Android編程解析Json格式數(shù)據(jù)的方法,涉及Android中json格式數(shù)據(jù)的構(gòu)造、讀取及遍歷等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • 分享Android中pullToRefresh的使用心得

    分享Android中pullToRefresh的使用心得

    這篇文章主要介紹了分享Android中pullToRefresh的使用心得的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 使用RecyclerView實(shí)現(xiàn)Item點(diǎn)擊事件

    使用RecyclerView實(shí)現(xiàn)Item點(diǎn)擊事件

    這篇文章主要介紹了使用RecyclerView實(shí)現(xiàn)Item點(diǎn)擊事件,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Android安卓中循環(huán)錄像并檢測內(nèi)存卡容量

    Android安卓中循環(huán)錄像并檢測內(nèi)存卡容量

    這篇文章主要介紹了Android安卓中循環(huán)錄像并檢測內(nèi)存卡容量,當(dāng)內(nèi)存卡空間已滿時(shí),本文還實(shí)現(xiàn)自動(dòng)刪除視頻列表里面的第一個(gè)文件,需要的朋友可以參考下
    2015-06-06
  • Android Studio實(shí)現(xiàn)格式化XML代碼順序

    Android Studio實(shí)現(xiàn)格式化XML代碼順序

    這篇文章主要介紹了Android Studio實(shí)現(xiàn)格式化XML代碼順序,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論

韩城市| 南丹县| 吴川市| 东安县| 漾濞| 浦县| 安多县| 无极县| 睢宁县| 崇左市| 贺州市| 宜宾市| 东兰县| 德保县| 灵宝市| 南川市| 贺州市| 屏南县| 辽阳市| 综艺| 翁牛特旗| 乌拉特后旗| 株洲市| 确山县| 西丰县| 玛曲县| 开江县| 石阡县| 常宁市| 陵川县| 大足县| 呼伦贝尔市| 望都县| 高陵县| 镇赉县| 安多县| 齐齐哈尔市| 临汾市| 荣成市| 普洱| 镇江市|