kotlin使用建造者模式自定义对话框

所属分类: 软件编程 / Android 阅读数: 47
收藏 0 赞 0 分享

本文实例为大家分享了kotlin自定义对话框的具体代码,供大家参考,具体内容如下

1.CommonDialog 创建我们自己的对话框,继承于系统的Dialog 实现构造方法

class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {}

2. 在内部创建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以外的区域时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("注销后需重新注册才能使用牛返返优惠")
        .setPositiveButton("确定注销", DialogInterface.OnClickListener { p0, p1 ->
          p0?.dismiss()
          DestroyAccount()
        })
        .setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
        .setWith(0.77f)
        .create()
        .show()

实现效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多