自定义输入框弹窗

  • Post author:
  • Post category:其他


在这里插入图片描述

如上图,自定义输入框弹窗,记录一下。

class InputPopupWindow(context: Context, content: String, type: Int) :
    PopupWindow() {
    private val mView: View
    private var mIvBack: ImageView? = null
    private var mEditText: EditText? = null
    private var mTvClear: TextView? = null
    private val mContext: Context
    private val mContent: String
    private val mInputType: Int
    private var mAutoFocusRunnable: Runnable? = null
    private fun setListener() {
        mIvBack!!.setOnClickListener { v: View? ->
            dismiss()
            if (mOnInputTextListener != null) {
                //退出输入框弹窗
                mOnInputTextListener!!.exitPopupWindow()
            }
        }
        //清理按钮
        mTvClear!!.setOnClickListener { v: View? ->
            mEditText!!.setText(
                ""
            )
        }
        mEditText!!.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event -> //监听editTextView的确认键
            LogUtil.i("event = $actionId")
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                val content = mEditText!!.text.toString()
                if (TextUtils.isEmpty(content) || TextUtils.equals(mContent, content)) {
                    mEditText!!.setText(mContent)
                    clearEtFocusable()
                    return@OnEditorActionListener false
                }
                if (mInputType == TYPE_HOTSPOT_PASSWORD || mInputType == TYPE_WIFI_PASSWORD) {
                    if (content.length < 8) {
                        return@OnEditorActionListener true
                    }
                }
                if (mOnInputTextListener != null) {
                    mOnInputTextListener!!.onInputText(content)
                }
                //清理焦点
                clearEtFocusable()
            }
            false
        })
        mEditText!!.addTextChangedListener(object : TextWatcher {
            var recordChangeAfter: String? = null
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                recordChangeAfter = s.toString()
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(editable: Editable) {
                val content = editable.toString()
                LogUtil.i("content = $content")
                if (TextUtils.isEmpty(content)) {
                    mTvClear!!.setTextColor(mContext.getColor(R.color.color_93B9F2))
                    mTvClear!!.alpha = 0.25f
                } else {
                    mTvClear!!.setTextColor(mContext.getColor(R.color.white))
                    mTvClear!!.alpha = 1.0f
                }
            }
        })
    }

    private fun initData() {
        if (TextUtils.isEmpty(mContent)) {
            mTvClear!!.setTextColor(mContext.getColor(R.color.color_93B9F2))
            mTvClear!!.alpha = 0.25f
        } else {
            mEditText!!.setText(mContent)
            mTvClear!!.setTextColor(mContext.getColor(R.color.white))
            mTvClear!!.alpha = 1.0f
            mEditText!!.setSelection(mContent.length)
        }
        if (mInputType == TYPE_BLUETOOTH) {
            mEditText!!.filters = arrayOf<InputFilter>(
                InputFilter.LengthFilter(12)
            )
        }
        if (mInputType == TYPE_HOTSPOT_ACCOUNT) {
            mEditText!!.filters = arrayOf<InputFilter>(
                InputFilter.LengthFilter(12)
            )
        }
        if (mInputType == TYPE_HOTSPOT_PASSWORD) {
            mEditText!!.filters = arrayOf(
                InputFilter.LengthFilter(16), SpaceFilter()
            )
            mEditText!!.inputType =
                InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        }
        if (mInputType == TYPE_ADD_MEMORY || mInputType == TYPE_UPDATE_MEMORY) {
            mEditText!!.filters = arrayOf<InputFilter>(
                InputFilter.LengthFilter(5)
            )
        }
        setEtFocusable()
    }

    private fun setEtFocusable() {
        mEditText!!.removeCallbacks(mAutoFocusRunnable)
        if (mAutoFocusRunnable == null) {
            mAutoFocusRunnable = Runnable {
                mEditText!!.isFocusable = true
                mEditText!!.isFocusableInTouchMode = true
                mEditText!!.requestFocus()
                val inputManager = mEditText!!.context
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputManager?.showSoftInput(mEditText, 0)
            }
        }
        mEditText!!.postDelayed(mAutoFocusRunnable, 500)
    }

    fun clearEtFocusable() {
        if (mEditText != null) {
            mEditText!!.isFocusable = false
            mEditText!!.removeCallbacks(mAutoFocusRunnable)
            val inputManager = mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager?.hideSoftInputFromWindow(mEditText!!.windowToken, 0)
            dismiss()
        }
    }

    fun show() {
        showAtLocation(mView, Gravity.TOP, 0, 0)
    }

    private fun initView(view: View) {
        mIvBack = view.findViewById(R.id.iv_back)
        mEditText = view.findViewById(R.id.edit_input)
        mTvClear = view.findViewById(R.id.tv_clear)
    }

    var mOnInputTextListener: OnInputTextListener? = null
    fun setOnInputTextListener(onInputTextListener: OnInputTextListener?) {
        mOnInputTextListener = onInputTextListener
    }

    interface OnInputTextListener {
        /**
         * 回调输入框内容
         *
         * @param text 输入框内容
         */
        fun onInputText(text: String?)
        fun exitPopupWindow() {}
    }

    inner class SpaceFilter : InputFilter {
        override fun filter(
            source: CharSequence,
            start: Int,
            end: Int,
            dest: Spanned,
            dstart: Int,
            dend: Int
        ): CharSequence? {
            // 判断是否是空格
            return if (source == " ") {
                ""
            } else null
        }
    }

    companion object {
        //主要用于判断输入框字数限制以及特殊处理
        const val TYPE_BLUETOOTH = 0x001
        const val TYPE_HOTSPOT_ACCOUNT = 0x002
        const val TYPE_HOTSPOT_PASSWORD = 0x003
        const val TYPE_WIFI_PASSWORD = 0x004
        const val TYPE_ADD_MEMORY = 0x005
        const val TYPE_UPDATE_MEMORY = 0x006
    }

    init {
        mContext = context
        mContent = content
        mInputType = type
        mView = LayoutInflater.from(context).inflate(R.layout.dialog_input, null)
        initView(mView)
        contentView = mView
        width = LinearLayout.LayoutParams.MATCH_PARENT
        height = LinearLayout.LayoutParams.MATCH_PARENT
        isFocusable = true
        isOutsideTouchable = false
        setBackgroundDrawable(BitmapDrawable())
        softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        initData()
        setListener()
    }
}



版权声明:本文为qq_44203816原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。