为什么在保留空间关键侦听器的情况下为ViewGroup添加addView时项目的背景为灰色?

时间:2018-09-06 06:26:33

标签: android kotlin keylistener

当我为此视图保留关键侦听器时

edit_text.setOnKeyListener(keyListener)

空格键

KeyEvent.KEYCODE_SPACE -> {
    if (listener != null && event.action == KeyEvent.ACTION_DOWN) {
        val text = edit_text.text
        if (text.isEmpty()) {
            return@OnKeyListener false
        }
        val startSelection = edit_text.selectionStart
        val endSelection = edit_text.selectionEnd

        if (startSelection < 0 || endSelection < 0) {
            return@OnKeyListener false
        }

        if (endSelection == startSelection) {
            val urlSpan = text.getSpans(0, startSelection, EditorUrlSpan::class.java)
            if (urlSpan?.isNotEmpty() == true) {
                val firstUrlSpan = urlSpan[0]
                val lastUrlSpan = urlSpan[urlSpan.size - 1]

                val firstStartIndex = text.getSpanStart(firstUrlSpan)
                val firstEndIndex = text.getSpanEnd(firstUrlSpan)
                val lastStartIndex = text.getSpanStart(lastUrlSpan)
                val lastEndIndex = text.getSpanEnd(lastUrlSpan)

                val character = when {
                    firstEndIndex < lastStartIndex -> text.subSequence(firstEndIndex, lastStartIndex)
                    firstStartIndex == lastStartIndex && firstEndIndex == lastEndIndex -> " "
                    else -> ""
                }
                when {
                    firstStartIndex == 0 && lastEndIndex >= endSelection && character == " " -> {
                        text.delete(lastStartIndex, lastEndIndex)
                        setText(text)
                        setSelection(length())
                        listener?.insertLink(this@KoalaEditTextView, lastUrlSpan.mLinkData.apply {
                            this.linkTitle = this.linkTitle?.replace("$href ", "")
                        })
                        return@OnKeyListener true
                    }

                    lastUrlSpan.mLinkData.linkTitle.isNullOrEmpty() -> {
                        sendParseUrlMessage(lastStartIndex, lastEndIndex, lastUrlSpan.mLinkData.linkUrl)
                        return@OnKeyListener true
                    }
                }

            }
        }
    }
    false
}

结束键代码

KeyEvent.KEYCODE_ENTER -> {
    if (listener != null && event.action == KeyEvent.ACTION_DOWN) {
        if (section == SECTION_NULL) {
            if (!quote) {
                listener!!.pressEnter(this@KoalaEditTextView)
                return@OnKeyListener true
            } else {
                val text = edit_text.text
                if (text.isEmpty()) {
                    setText("")
                    listener!!.pressEnter(this@KoalaEditTextView)
                    return@OnKeyListener true
                } else if (edit_text.selectionEnd == text.length && text.toString()[text.length - 1] == '\n') {
                    setText(text.subSequence(0, text.toString().length - 1))
                    setSelection(edit_text.length())
                    listener!!.pressEnter(this@KoalaEditTextView)
                    return@OnKeyListener true
                }
                if (edit_text.selectionStart == edit_text.selectionEnd && edit_text.selectionStart == 1
                    && text.toString()[0] == '\n') {
                    setText(text.subSequence(1, text.length))
                    listener!!.insertEdit(this@KoalaEditTextView)
                    return@OnKeyListener true
                }
            }
        } else {
            if (edit_text.text.toString().trim { it <= ' ' }.isEmpty()) {
                cleanSection(this@KoalaEditTextView)
                resetPosition()
                return@OnKeyListener true
            } else {
                listener!!.pressEnter(this@KoalaEditTextView)
                return@OnKeyListener true
            }
        }
        return@OnKeyListener false
    }
    true
}

和侦听器insertLink并按Enter

override fun insertLink(v: KoalaEditTextView, linkData: LinkData) {
    val index = container.indexOfChild(v)
    val linkView = KoalaLinkView(context, linkData)
    linkView.setOnLinkClickListener(itemLinkListener)
    val lpCard = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    container.addView(linkView, index + 1, lpCard)
    linkView.requestFocus()
    linkView.post {
        linkView.link_right_area.requestFocus()
        linkView.link_right_area.performClick()
    }
}

override fun pressEnter(v: KoalaEditTextView) {
    var leftSelectionText: CharSequence
    var rightSelectionText: CharSequence
    val index = container.indexOfChild(v)
    val startSelection = v.selectionStart
    val endSelection = v.selectionStart
    val txt = SpannableStringBuilder(v.obtainText())
    if (startSelection < v.length()) {
        leftSelectionText = txt.subSequence(0, if (startSelection > -1) {
            startSelection
        } else {
            0
        })
        rightSelectionText = txt.subSequence(if (startSelection > -1) {
            startSelection
        } else {
            0
        }, v.length())
    } else {
        leftSelectionText = txt
        rightSelectionText = ""
    }

    val urlSpan = txt.getSpans(0, startSelection, KoalaEditTextView.Companion.EditorUrlSpan::class.java)
    if (urlSpan?.isNotEmpty() == true) {
        val firstUrlSpan = urlSpan[0]
        val lastUrlSpan = urlSpan[urlSpan.size - 1]

        val firstStartIndex = txt.getSpanStart(firstUrlSpan)
        val firstEndIndex = txt.getSpanEnd(firstUrlSpan)
        val lastStartIndex = txt.getSpanStart(lastUrlSpan)
        val lastEndIndex = txt.getSpanEnd(lastUrlSpan)

        val character = when {
            firstEndIndex < lastStartIndex -> txt.subSequence(firstEndIndex, lastStartIndex)
            firstStartIndex == lastStartIndex && firstEndIndex == lastEndIndex -> "\n"
            else -> ""
        }
        when {
            firstStartIndex == 0 && startSelection > 0 && lastEndIndex >= endSelection && character == "\n" -> {
                txt.delete(lastStartIndex, lastEndIndex)
                v.setText(txt)
                v.setSelection(v.length())
                insertLink(v, lastUrlSpan.mLinkData.apply {
                    this.linkTitle = this.linkTitle?.replace("$href ", "")
                })
                return
            }

            lastUrlSpan.mLinkData.linkTitle.isNullOrEmpty() -> {
                v.sendParseUrlMessage(lastStartIndex, lastEndIndex, lastUrlSpan.mLinkData.linkUrl)
                return
            }

            endSelection > -1 && lastEndIndex > endSelection -> {
                leftSelectionText = txt.subSequence(0, lastStartIndex)
                rightSelectionText = txt.subSequence(lastStartIndex, v.length())
            }
        }
    }

    v.setText(leftSelectionText)
    val editTextView = KoalaEditTextView(context, this, menuStatusListener, contentListener)
    val lp = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    container.addView(editTextView, index + 1, lp)
    editTextView.setText(rightSelectionText)
    editTextView.requestFocus()
    editTextView.setSelection(0)
    if (v.obtainSection() == SECTION_NUMBER) {
        editTextView.setNumberSection(editTextView)
    } else if (v.obtainSection() == SECTION_DOT) {
        editTextView.setDotSection(editTextView)
    }
    editTextView.resetPosition()
    setHint()
}

当我按空格键并输入时,代码已调用

add view for space key

add view for enter key

调用相同的代码,并且空格键显示为灰色bg,Enter键不是 the gray background

该如何解决?谢谢!

1 个答案:

答案 0 :(得分:0)

现在,我通过查看InputMethodService.class的android源代码解决了它

在我的代码中,我已实现InputConnectionWrapper来覆盖此代码的commitText

 override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
            return when {
                text?.toString() == "\n" -> sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER)
                text?.toString() == " " -> sendDownUpKeyEvents(KeyEvent.KEYCODE_SPACE)
                else -> super.commitText(text, newCursorPosition)
            }
        }

        private fun sendDownUpKeyEvents(keyCode: Int): Boolean {
            val eventTime = SystemClock.uptimeMillis()
            return sendKeyEvent(KeyEvent(eventTime, eventTime,
                KeyEvent.ACTION_DOWN, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE))
                && sendKeyEvent(KeyEvent(eventTime, eventTime,
                KeyEvent.ACTION_UP, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE))
        }

sendDownUpKeyEvents发生了一些错误,此错误发生在我使用的地方

  

text?.toString()==“”-> sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SPACE))                           && sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_SPACE))

我阅读了标志中KeyEvent.class构造函数参数的原因 KeyEvent.FLAG_KEEP_TOUCH_MODE对于此错误更重要!