SelectableInput.kt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.grkj.iscs.widget
  2. import android.content.Context
  3. import android.util.AttributeSet
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.view.inputmethod.InputMethodManager
  8. import android.widget.LinearLayout
  9. import android.widget.PopupWindow
  10. import androidx.appcompat.content.res.AppCompatResources
  11. import com.grkj.iscs.R
  12. import com.grkj.iscs.databinding.LayoutSelectableInputBinding
  13. import com.grkj.iscs.databinding.LayoutSelectableinputSpinnerBinding
  14. import com.zhy.adapter.recyclerview.CommonAdapter
  15. import com.zhy.adapter.recyclerview.base.ViewHolder
  16. class SelectableInput(private val ctx: Context, attrs: AttributeSet) : LinearLayout(ctx, attrs) {
  17. companion object {
  18. const val MODE_INPUT = 0
  19. const val MODE_SELECT = 1
  20. }
  21. private var mBinding: LayoutSelectableInputBinding
  22. private var mDropdownView: View? = null
  23. private var mPopWindow: PopupWindow? = null
  24. var mOptionList = mutableListOf<String>()
  25. private var isSkipListener: Boolean = true
  26. private var mSelectListener: OnSpinnerSelectListener? = null
  27. init {
  28. val root = View.inflate(ctx, R.layout.layout_selectable_input, this)
  29. mBinding = LayoutSelectableInputBinding.bind(root)
  30. val attrSet = ctx.obtainStyledAttributes(attrs, R.styleable.SelectableInput)
  31. val mode = attrSet.getInt(R.styleable.SelectableInput_mode, 0)
  32. setMode(mode)
  33. val isRequired = attrSet.getBoolean(R.styleable.SelectableInput_required, false)
  34. mBinding.tvPrefix.visibility = if (isRequired) View.VISIBLE else View.INVISIBLE
  35. val isEnabled = attrSet.getBoolean(R.styleable.SelectableInput_enabled, true)
  36. mBinding.et.isEnabled = isEnabled
  37. mBinding.tvName.text = attrSet.getString(R.styleable.SelectableInput_name)
  38. mBinding.et.hint = attrSet.getString(R.styleable.SelectableInput_edittext_hint)
  39. attrSet.recycle()
  40. }
  41. private fun setMode(mode: Int) {
  42. if (mode == MODE_SELECT) {
  43. mBinding.et.background = AppCompatResources.getDrawable(ctx, R.drawable.selectable_input_spinner_bg)
  44. // mBinding.et.isEnabled = false
  45. mBinding.et.inputType = 0
  46. // btnDropdown.visibility = View.VISIBLE
  47. mBinding.et.setOnClickListener {
  48. println("haha : ${mOptionList.isNotEmpty()}")
  49. if (mOptionList.isNotEmpty()) {
  50. showDropdown()
  51. }
  52. }
  53. } else {
  54. mBinding.et.background = AppCompatResources.getDrawable(ctx, R.drawable.selectable_input_text_bg)
  55. mBinding.et.isEnabled = true
  56. // btnDropdown.visibility = View.GONE
  57. // btnDropdown.setOnClickListener(null)
  58. // clContainer.setOnClickListener {
  59. // editText.requestFocus()
  60. // val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  61. // imm.showSoftInput(editText, 0)
  62. // }
  63. mBinding.et.setOnFocusChangeListener { view, b ->
  64. if (!b) {
  65. val imm = ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  66. imm.hideSoftInputFromWindow(view.windowToken, 0)
  67. }
  68. }
  69. }
  70. }
  71. fun setText(v: String?) {
  72. isSkipListener = true
  73. mBinding.et.setText(v)
  74. v?.let {
  75. mBinding.et.setSelection(it.length)
  76. }
  77. isSkipListener = false
  78. }
  79. private fun showDropdown() {
  80. mDropdownView?:let {
  81. mDropdownView = LayoutInflater.from(ctx).inflate(R.layout.layout_selectableinput_spinner, null)
  82. }
  83. mPopWindow ?: let {
  84. mPopWindow = PopupWindow(context)
  85. mPopWindow?.contentView = mDropdownView
  86. mPopWindow?.width = mBinding.et.width
  87. mPopWindow?.height = ViewGroup.LayoutParams.WRAP_CONTENT
  88. mPopWindow?.isOutsideTouchable = true
  89. mPopWindow?.isFocusable = true
  90. val popBinding = LayoutSelectableinputSpinnerBinding.bind(mDropdownView!!)
  91. popBinding.rvOptions.adapter = object : CommonAdapter<String>(ctx, R.layout.item_rv_selectableinput_spinner, mOptionList) {
  92. override fun convert(holder: ViewHolder?, option: String?, position: Int) {
  93. holder?.setText(R.id.tv_option, option)
  94. holder?.setOnClickListener(R.id.tv_option) {
  95. mBinding.et.setText(option)
  96. mSelectListener?.onSelect(option, position)
  97. mPopWindow?.dismiss()
  98. }
  99. }
  100. }
  101. }
  102. if (mPopWindow?.isShowing == false) {
  103. mPopWindow?.showAsDropDown(mBinding.et)
  104. } else {
  105. mPopWindow?.dismiss()
  106. }
  107. }
  108. fun setOnSpinnerSelectListener(onSpinnerSelectListener: OnSpinnerSelectListener) {
  109. mSelectListener = onSpinnerSelectListener
  110. }
  111. interface OnSpinnerSelectListener {
  112. fun onSelect(str: String?, index: Int)
  113. }
  114. }