如何在用户输入时在EditText中添加哈希标签

我在编辑文本中创建hashTag时遇到了问题。

以下是我的需求

1.每当用户开始键入时,它都以hash开头。如果用户键入“G”,则整个单词将类似于“#g”。

2.键入word和user后,输入空格并键入“T”,然后键入“#t”。

我试过使用text change listner,但它对我不起作用。

我尝试过使用textwatcher:

tt = object:TextWatcher {
            override fun afterTextChanged(s: Editable) {
                edtTags.setSelection(s.length)
            }
            override fun beforeTextChanged(s:CharSequence, start:Int, count:Int, after:Int) {}
            override  fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
                edtTags.removeTextChangedListener(tt)
                edtTags.setText(edtTags.getText().toString().replace(" ", " #"))
                edtTags.addTextChangedListener(tt)
            }
        }
        edtTags.addTextChangedListener(tt)

谢谢!

转载请注明出处:http://www.shenzhenzsmy.com/article/20230526/2303280.html