查看: 518|回复: 0

[手机开发] Kotlin用建造者模式创建对话框

发表于 2018-2-4 10:14:58
  1. class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {
  2. /**
  3. * 建造器的构造方法:
  4. *
  5. * @param context
  6. */
  7. class Builder (private val context: Context) {
  8. private var title: String? = null
  9. private var message: String? = null
  10. private var positiveButtonContent: String? = null
  11. private var negativeButtonContent: String? = null
  12. private var positiveButtonListener: DialogInterface.OnClickListener? = null
  13. private var negativeButtonListener: DialogInterface.OnClickListener? = null
  14. private var contentView: View? = null
  15. private var imageid: Int = 0
  16. private var color: Int = 0
  17. private var withOffSize: Float = 0.toFloat()
  18. private var heightOffSize: Float = 0.toFloat()
  19. fun setTitle(title: String): Builder {
  20. this.title = title
  21. return this
  22. }
  23. fun setTitle(title: Int): Builder {
  24. this.title = context.getText(title) as String
  25. return this
  26. }
  27. fun setMessage(message: String): Builder {
  28. this.message = message
  29. return this
  30. }
  31. fun setMessageColor(color: Int): Builder {
  32. this.color = color
  33. return this
  34. }
  35. fun setImageHeader(Imageid: Int): Builder {
  36. this.imageid = Imageid
  37. return this
  38. }
  39. fun setPositiveButton(text: String, listener: DialogInterface.OnClickListener): Builder {
  40. this.positiveButtonContent = text
  41. this.positiveButtonListener = listener
  42. return this
  43. }
  44. fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
  45. this.positiveButtonContent = context.getText(textId) as String
  46. this.positiveButtonListener = listener
  47. return this
  48. }
  49. fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {
  50. this.negativeButtonContent = text
  51. this.negativeButtonListener = listener
  52. return this
  53. }
  54. fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
  55. this.negativeButtonContent = context.getText(textId) as String
  56. this.negativeButtonListener = listener
  57. return this
  58. }
  59. fun setContentView(v: View): Builder {
  60. this.contentView = v
  61. return this
  62. }
  63. fun setWith(v: Float): Builder {
  64. this.withOffSize = v
  65. return this
  66. }
  67. fun setContentView(v: Float): Builder {
  68. this.heightOffSize = v
  69. return this
  70. }
  71. fun create(): CommonDialog {
  72. /**
  73. * 利用我们刚才自定义的样式初始化Dialog
  74. */
  75. val dialog = CommonDialog(context,
  76. R.style.dialogStyle)
  77. /**
  78. * 下面就初始化Dialog的布局页面
  79. */
  80. val inflater = context
  81. .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
  82. val dialogLayoutView = inflater.inflate(R.layout.dialog_layout,
  83. null)
  84. dialog.addContentView(dialogLayoutView, ViewGroup.LayoutParams(
  85. ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
  86. if (imageid != 0) {
  87. (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView)
  88. .setImageResource(imageid)
  89. } else {
  90. (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView).visibility = View.GONE
  91. }
  92. if (!TextUtils.isEmpty(title)) {
  93. (dialogLayoutView.findViewById<View>(R.id.tv_dialog_title) as TextView).text = title
  94. } else {
  95. // Log.w(context.getClass().toString(), "未设置对话框标题!");
  96. }
  97. if (color != 0) {
  98. val viewById = dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView
  99. viewById.setTextColor(color)
  100. }
  101. if (!TextUtils.isEmpty(message)) {
  102. (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).text = message
  103. } else if (contentView != null) {
  104. (dialogLayoutView
  105. .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout)
  106. .removeAllViews()
  107. (dialogLayoutView
  108. .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout).addView(
  109. contentView, ViewGroup.LayoutParams(
  110. ViewGroup.LayoutParams.WRAP_CONTENT,
  111. ViewGroup.LayoutParams.WRAP_CONTENT))
  112. } else {
  113. (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).visibility = View.INVISIBLE
  114. }
  115. if (!TextUtils.isEmpty(positiveButtonContent)) {
  116. (dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).text = positiveButtonContent
  117. if (positiveButtonListener != null) {
  118. (dialog.findViewById<View>(R.id.tv_dialog_pos) as TextView)
  119. .setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }
  120. }
  121. } else {
  122. (dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).visibility = View.GONE
  123. dialogLayoutView.findViewById<View>(R.id.line).visibility = View.GONE
  124. }
  125. if (!TextUtils.isEmpty(negativeButtonContent)) {
  126. (dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).text = negativeButtonContent
  127. if (negativeButtonListener != null) {
  128. (dialogLayoutView
  129. .findViewById<View>(R.id.tv_dialog_neg) as TextView)
  130. .setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }
  131. }
  132. } else {
  133. (dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).visibility = View.GONE
  134. }
  135. /**
  136. * 将初始化完整的布局添加到dialog中
  137. */
  138. dialog.setContentView(dialogLayoutView)
  139. /**
  140. * 禁止点击Dialog以外的区域时Dialog消失
  141. */
  142. dialog.setCanceledOnTouchOutside(false)
  143. val window = dialog.window
  144. val context = this.context as Activity
  145. val windowManager = context.windowManager
  146. val defaultDisplay = windowManager.defaultDisplay
  147. val attributes = window!!.attributes
  148. if (withOffSize.toDouble() != 0.0) {
  149. attributes.width = (defaultDisplay.width * withOffSize).toInt()
  150. } else {
  151. attributes.width = (defaultDisplay.width * 0.77).toInt()
  152. }
  153. if (heightOffSize.toDouble() != 0.0) {
  154. attributes.height = (defaultDisplay.height * heightOffSize).toInt()
  155. }
  156. window.attributes = attributes
  157. return dialog
  158. }
  159. }
  160. }
复制代码

使用:

  1. CommonDialog.Builder(this).
  2. setImageHeader(R.mipmap.icon_gantan_tankuang)
  3. .setTitle("你是否要注销账户")
  4. .setMessage("注销后需重新注册才能使用牛返返优惠")
  5. .setPositiveButton("确定注销", DialogInterface.OnClickListener { p0, p1 ->
  6. p0?.dismiss()
  7. DestroyAccount()
  8. })
  9. .setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
  10. .setWith(0.77f)
  11. .create()
  12. .show()
复制代码

效果图:
图片描述



回复

使用道具 举报