DialogInput.kt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package com.cr.dialog
  2. import android.app.Dialog
  3. import android.content.Context
  4. import android.view.Gravity
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.widget.Button
  8. import android.widget.LinearLayout
  9. import android.widget.TextView
  10. import com.cr.cruav.R
  11. import com.cr.data.CrUtil
  12. import com.cr.widget.CrEditTextWidget
  13. /**
  14. * 操作系统:MAC系统
  15. * 创建者:王成
  16. * 创建日期:2023/6/7 15:42
  17. * 描述:输入对话框
  18. */
  19. class DialogInput : Dialog, View.OnClickListener {
  20. /**
  21. * 对外接口
  22. */
  23. interface DialogInputListener {
  24. /**
  25. * 完成
  26. * @param valueOne String 值1
  27. * @param valueTwo String 值2
  28. * @param self DialogInput 对话框本身
  29. */
  30. fun completion(valueOne:String,valueTwo:String,self:DialogInput)
  31. /**
  32. * 关闭
  33. */
  34. fun close()
  35. }
  36. // define: 2023/3/31 监听接口
  37. private var listener: DialogInputListener? = null
  38. // define: 2023/3/31 定义控件
  39. var btnCompletion: Button? = null // define: 2023/6/7 完成按钮
  40. var btnClose: Button? = null // define: 2023/6/7 取消按钮
  41. var lblTitle: TextView? = null // define: 2023/6/7 对话框标题
  42. var lblOne: TextView? = null // define: 2023/6/7 第一输入框标题
  43. var lblTwo: TextView? = null // define: 2023/6/7 第二输入框标题
  44. var panelOne: LinearLayout? = null // define: 2023/6/7 第一输入框容器
  45. var panelTwo: LinearLayout? = null // define: 2023/6/7 第二输入框容器
  46. var txtOne: CrEditTextWidget? = null // define: 2023/6/7 第一输入框
  47. var txtTwo: CrEditTextWidget? = null // define: 2023/6/7 第二输入框
  48. var self:DialogInput?=null // define: 2023/6/7 自身引用
  49. /**
  50. * 初始化
  51. * @param context Context
  52. * @constructor
  53. */
  54. constructor(context: Context) : this(context, 0)
  55. /**
  56. * 初始化
  57. * @param context Context 上下文
  58. * @param themeResId Int 主题Id
  59. * @constructor
  60. */
  61. constructor(context: Context, themeResId: Int) : super(context, R.style.Dialog) {
  62. init()
  63. }
  64. /**
  65. * 初始化
  66. * @param context Context 上下文
  67. * @param title String 标题
  68. * @constructor
  69. */
  70. constructor(context: Context, title: String) : super(context, R.style.Dialog) {
  71. init()
  72. lblTitle?.text = title
  73. }
  74. /**
  75. * 初始化
  76. * @param context Context 上下文
  77. * @param title String 主题
  78. * @param listener DialogNormalListener 消息监听
  79. * @constructor
  80. */
  81. constructor(
  82. context: Context,
  83. title: String,
  84. listener: DialogInputListener
  85. ) : super(context, R.style.Dialog) {
  86. init()
  87. lblTitle?.text = title
  88. this.listener = listener
  89. }
  90. /**
  91. * 初始化
  92. */
  93. private fun init() {
  94. self = this
  95. setContentView(R.layout.dig_input)
  96. window?.setGravity(Gravity.CENTER)
  97. window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
  98. // todo: 2023/3/31 禁止点击对话框外部关闭
  99. setCanceledOnTouchOutside(false)
  100. // todo: 2023/3/31 初始化控件
  101. btnCompletion = findViewById(R.id.dig_btn_ok)
  102. btnClose = findViewById(R.id.dig_btn_cancel)
  103. btnCompletion?.setOnClickListener(this)
  104. btnClose?.setOnClickListener(this)
  105. lblTitle = findViewById(R.id.dig_title)
  106. lblOne = findViewById(R.id.dig_lbl_one)
  107. lblTwo = findViewById(R.id.dig_lbl_two)
  108. panelOne = findViewById(R.id.dig_panel_one)
  109. panelTwo = findViewById(R.id.dig_panel_two)
  110. txtOne = findViewById(R.id.dig_txt_one)
  111. txtTwo = findViewById(R.id.dig_txt_two)
  112. // todo: 2023/3/31 默认不显示取消按钮
  113. btnClose?.visibility = View.GONE
  114. }
  115. /**
  116. * 设置标题
  117. * @param title String 标题内容
  118. */
  119. fun setTitle(title: String) {
  120. lblTitle?.text = title
  121. }
  122. /**
  123. * 设置标签显示内容
  124. * @param strOne String 标签一内容
  125. * @param strTow String 标签二内容
  126. */
  127. fun setLabels(strOne: String,strTow:String?) {
  128. if(strOne == null){
  129. panelOne?.visibility = View.GONE
  130. }else{
  131. panelOne?.visibility = View.VISIBLE
  132. lblOne?.text = strOne
  133. }
  134. if(strTow == null){
  135. panelTwo?.visibility = View.GONE
  136. }else{
  137. panelTwo?.visibility = View.VISIBLE
  138. lblTwo?.text = strTow
  139. }
  140. }
  141. /**
  142. * 设置输入文本框的提示信息
  143. * @param hintOne String 输入框一的提示信息
  144. * @param hintTwo String 输入框二的提示信息
  145. */
  146. fun setHints(hintOne:String,hintTwo:String){
  147. txtOne?.setContextHint(hintOne)
  148. txtTwo?.setContextHint(hintTwo)
  149. }
  150. /**
  151. * 设置输入框图标
  152. * @param fontOne String 输入框一图标
  153. * @param fontTwo String 输入框二图标
  154. */
  155. fun setFonts(fontOne:String,fontTwo:String){
  156. txtOne?.setFontIcon(fontOne)
  157. txtTwo?.setFontIcon(fontTwo)
  158. }
  159. /**
  160. * 设置按钮文字
  161. * @param okText String 确定按钮文字
  162. * @param closeText String 关闭按钮文字
  163. */
  164. fun setButtonsText(okText: String, closeText: String) {
  165. if (okText != null) {
  166. btnCompletion?.text = okText
  167. btnCompletion?.visibility = View.VISIBLE
  168. } else {
  169. btnCompletion?.visibility = View.GONE
  170. }
  171. if (closeText != null) {
  172. btnClose?.text = closeText
  173. btnClose?.visibility = View.VISIBLE
  174. } else {
  175. btnClose?.visibility = View.GONE
  176. }
  177. }
  178. /**
  179. * 设置初始化显示内容
  180. * @param onText String
  181. * @param twoText String
  182. */
  183. fun setContent(onText:String,twoText:String){
  184. txtOne?.setContent(onText)
  185. txtTwo?.setContent(twoText)
  186. }
  187. /**
  188. * 设置
  189. * @param okVisible Boolean 确定按钮是否显示
  190. * @param closeVisible Boolean 取消按钮是否显示
  191. */
  192. fun setButtonsVisible(okVisible: Boolean, closeVisible: Boolean) {
  193. btnCompletion?.visibility = if (okVisible) View.VISIBLE else View.GONE
  194. btnClose?.visibility = if (closeVisible) View.VISIBLE else View.GONE
  195. }
  196. /**
  197. * 设置监听
  198. * @param listener DialogInputListener
  199. */
  200. fun setListener(listener: DialogInputListener) {
  201. this.listener = listener
  202. }
  203. /**
  204. * 覆写点击事件
  205. * @param p0 View
  206. */
  207. override fun onClick(p0: View?) {
  208. when (p0?.id) {
  209. R.id.dig_btn_ok -> {
  210. if(panelOne?.visibility == View.VISIBLE && (txtOne!!.getContent() == null || txtOne!!.getContent() == "")){
  211. CrUtil.showToast("请输入内容!")
  212. return
  213. }
  214. if(panelTwo?.visibility == View.VISIBLE && (txtTwo!!.getContent() == null || txtTwo!!.getContent() == "")){
  215. CrUtil.showToast("请输入内容!")
  216. return
  217. }
  218. dismiss()
  219. listener?.completion(txtOne!!.getContent(),txtTwo!!.getContent(),self!!)
  220. }
  221. R.id.dig_btn_cancel -> {
  222. dismiss()
  223. listener?.close()
  224. }
  225. }
  226. }
  227. }