把 # 放在 written edit text 之前

put # before the word written edit text

如何在 edittext 中的单词前放置 # 字符?

我想要的:我想在编辑文本中写的每个字前加上 # 个字符

每个单词都以#开头

例如:

#hello #world #hi


试试这个 TextWatcher

class SharpWordsTextWatcher: TextWatcher {

    private var addSharp: Boolean = false
    private var isEmpty = false

    override fun afterTextChanged(s: Editable) {
        if(addSharp) {
            s.insert(s.length - 1, "#")
        }
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        isEmpty = s.isEmpty() || s.last() == ' '
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        addSharp = isEmpty && s.last() != ' ' && count != 0
    }

}

并将此添加到您的 EditText

myEditText.addTextChangedListener(SharpWordsTextWatcher())