Когда я сохраняю ключевой приемник для этого представления
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()
}
что-то случилось, когда я нажимаю пробел и ввод, код вызвал
добавить вид для клавиши пробела
добавить виддля клавиши ввода
вызывается тот же код, а клавиша пробела получает серый bg, клавиша ввода не серый фон
Как решить эту проблему?Спасибо!