输入时@mentions和#hashtags的Android edittext颜色更改

时间:2017-11-29 06:34:00

标签: android kotlin

我有一个edittext,当用户输入时, @mentions #hashtags 的颜色应该会改变。我使用过textwatcher,但它很迟钝,在输入时错过了一些字符。我很想知道替代方案。

这是我的代码:

var previousString = ""
        override fun initTextAreaView() {
            if(twitterAvailable && twitterIsEnabled) CHARLIMIT = 280
            if(!twitterIsEnabled && linkedInAvailable && linkedInIsEnabled) CHARLIMIT = 700
            charcount.text = CHARLIMIT.toString()
            content.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {}
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    if(s.toString()!=previousString)checkMentionsAndHashtags(s.toString())

                }
            })
        }

        var textSpanList: ArrayList<TextSpan> = ArrayList()
        private fun checkMentionsAndHashtags(s: String) {
            previousString = s
            textSpanList = ArrayList()
            val words = s.split(" ")
            for(word in words){
                if(word.length>0) {
                    if (word[0] == '@' || word[0] == '#') {
                        val n = s.lastIndexOf(word)
                        textSpanList.add(TextSpan(n, (n+word.length)))
                    }
                }
            }
            changeTextColor(s, SpannableStringBuilder(s))
        }

        private fun changeTextColor(s: String = "", str: SpannableStringBuilder = SpannableStringBuilder("")) {
            Log.d("TEXTSPANS", textSpanList.toString())
            if(textSpanList.size>0){
                for(span in textSpanList) {
                    str.setSpan(ForegroundColorSpan(resources.getColor(R.color.colorPrimary)), span.startPos, span.endPos, 0)
                }
            }
            val cursorPos = content.selectionEnd
            content.text = str
            content.setSelection(cursorPos)
        }

1 个答案:

答案 0 :(得分:0)

你必须尝试这样

  lateinit var content:EditText
    private var intCount = 0
    private var initialStringLength = 0
    private var strText = ""

    private fun initTextAreaView() {
        content= findViewById(R.id.testText)
        content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val typedString = s.toString()
                val stringArrayObj = typedString.split(" ".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
                var cnt = 0
                if (typedString.length != initialStringLength && typedString.length != 0) {
                    if (typedString.substring(typedString.length - 1) != " ") {
                        initialStringLength = typedString.length
                        cnt = intCount
                        for (i in stringArrayObj.indices)
                            if (stringArrayObj[i].get(0) == '#' || stringArrayObj[i].get(0) == '@') {
                                 strText = strText + " " + "<font color='#EE0000'>" + stringArrayObj[i] + "</font>"
                            }
                            else
                                strText = strText + " " + stringArrayObj[i]
                    }
                    if (intCount == cnt) {
                        intCount = stringArrayObj.size
                        content.setText(Html.fromHtml(strText))
                        content.setSelection(content.getText().toString().length)
                    }
                } else {
                    strText = ""
                }

            }
        })
    }