| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package com.grkj.iscs.widget
- import android.content.Context
- import android.util.AttributeSet
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.view.inputmethod.InputMethodManager
- import android.widget.LinearLayout
- import android.widget.PopupWindow
- import androidx.appcompat.content.res.AppCompatResources
- import com.grkj.iscs.R
- import com.grkj.iscs.databinding.LayoutSelectableInputBinding
- import com.grkj.iscs.databinding.LayoutSelectableinputSpinnerBinding
- import com.zhy.adapter.recyclerview.CommonAdapter
- import com.zhy.adapter.recyclerview.base.ViewHolder
- class SelectableInput(private val ctx: Context, attrs: AttributeSet) : LinearLayout(ctx, attrs) {
- companion object {
- const val MODE_INPUT = 0
- const val MODE_SELECT = 1
- }
- private var mBinding: LayoutSelectableInputBinding
- private var mDropdownView: View? = null
- private var mPopWindow: PopupWindow? = null
- var mOptionList = mutableListOf<String>()
- private var isSkipListener: Boolean = true
- private var mSelectListener: OnSpinnerSelectListener? = null
- init {
- val root = View.inflate(ctx, R.layout.layout_selectable_input, this)
- mBinding = LayoutSelectableInputBinding.bind(root)
- val attrSet = ctx.obtainStyledAttributes(attrs, R.styleable.SelectableInput)
- val mode = attrSet.getInt(R.styleable.SelectableInput_mode, 0)
- setMode(mode)
- val isRequired = attrSet.getBoolean(R.styleable.SelectableInput_required, false)
- mBinding.tvPrefix.visibility = if (isRequired) View.VISIBLE else View.INVISIBLE
- val isEnabled = attrSet.getBoolean(R.styleable.SelectableInput_enabled, true)
- mBinding.et.isEnabled = isEnabled
- mBinding.tvName.text = attrSet.getString(R.styleable.SelectableInput_name)
- mBinding.et.hint = attrSet.getString(R.styleable.SelectableInput_edittext_hint)
- attrSet.recycle()
- }
- private fun setMode(mode: Int) {
- if (mode == MODE_SELECT) {
- mBinding.et.background = AppCompatResources.getDrawable(ctx, R.drawable.selectable_input_spinner_bg)
- // mBinding.et.isEnabled = false
- mBinding.et.inputType = 0
- // btnDropdown.visibility = View.VISIBLE
- mBinding.et.setOnClickListener {
- println("haha : ${mOptionList.isNotEmpty()}")
- if (mOptionList.isNotEmpty()) {
- showDropdown()
- }
- }
- } else {
- mBinding.et.background = AppCompatResources.getDrawable(ctx, R.drawable.selectable_input_text_bg)
- mBinding.et.isEnabled = true
- // btnDropdown.visibility = View.GONE
- // btnDropdown.setOnClickListener(null)
- // clContainer.setOnClickListener {
- // editText.requestFocus()
- // val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- // imm.showSoftInput(editText, 0)
- // }
- mBinding.et.setOnFocusChangeListener { view, b ->
- if (!b) {
- val imm = ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- imm.hideSoftInputFromWindow(view.windowToken, 0)
- }
- }
- }
- }
- fun setText(v: String?) {
- isSkipListener = true
- mBinding.et.setText(v)
- v?.let {
- mBinding.et.setSelection(it.length)
- }
- isSkipListener = false
- }
- private fun showDropdown() {
- mDropdownView?:let {
- mDropdownView = LayoutInflater.from(ctx).inflate(R.layout.layout_selectableinput_spinner, null)
- }
- mPopWindow ?: let {
- mPopWindow = PopupWindow(context)
- mPopWindow?.contentView = mDropdownView
- mPopWindow?.width = mBinding.et.width
- mPopWindow?.height = ViewGroup.LayoutParams.WRAP_CONTENT
- mPopWindow?.isOutsideTouchable = true
- mPopWindow?.isFocusable = true
- val popBinding = LayoutSelectableinputSpinnerBinding.bind(mDropdownView!!)
- popBinding.rvOptions.adapter = object : CommonAdapter<String>(ctx, R.layout.item_rv_selectableinput_spinner, mOptionList) {
- override fun convert(holder: ViewHolder?, option: String?, position: Int) {
- holder?.setText(R.id.tv_option, option)
- holder?.setOnClickListener(R.id.tv_option) {
- mBinding.et.setText(option)
- mSelectListener?.onSelect(option, position)
- mPopWindow?.dismiss()
- }
- }
- }
- }
- if (mPopWindow?.isShowing == false) {
- mPopWindow?.showAsDropDown(mBinding.et)
- } else {
- mPopWindow?.dismiss()
- }
- }
- fun setOnSpinnerSelectListener(onSpinnerSelectListener: OnSpinnerSelectListener) {
- mSelectListener = onSpinnerSelectListener
- }
- interface OnSpinnerSelectListener {
- fun onSelect(str: String?, index: Int)
- }
- }
|