在Android Java中键入时,Edittext中哈希标记的自动着色

在Android Java中键入时,Edittext中哈希标记的自动着色,java,android,android-edittext,highlight,Java,Android,Android Edittext,Highlight,因此,我的布局中有一个“EditText”,用于简单的文本输入。 当我编写一个标签时,例如“你好”,我希望该标签以蓝色突出显示 editText.setOnTextChangedListener(...) { public void afterTextChanged(...) // replace current String with editText.setText(Html.parse(...)); } 我已经尝试了一些方法来实现这一点——在我的PostTextCh

因此,我的布局中有一个“EditText”,用于简单的文本输入。 当我编写一个标签时,例如“你好”,我希望该标签以蓝色突出显示

editText.setOnTextChangedListener(...) {
   public void afterTextChanged(...)
        // replace current String with editText.setText(Html.parse(...));
}
我已经尝试了一些方法来实现这一点——在我的PostTextChangedListener中使用Spannablestring和基于HTML的SetText——我在这里遇到的唯一问题是,当我尝试键入内容时,光标跳到了第一个位置——所以实时高亮显示在Android Java中不起作用,我想——至少在我的算法中不起作用


有什么建议吗?

因为您已经知道如何着色,我将建议您如何使用sunil sunny建议的方法更改字符串,使光标保持在指定位置

editText.setOnTextChangedListener(...) {
   public void afterTextChanged(...)
        int currentSelectionStart = editText.getSelectionStart()
        int currentSelectionEnd = editText.getSelectionEnd()
        // replace current String with Spannable for every "Hashtag"
        editText.setSelection(currentCursorPosition,currentSelectionEnd )
}

如果光标跳到第一个位置是您真正的问题,那么这可以通过以下方法解决:
editText.setSelection(editText.getText().length())。使用这个代码后,你已经做了颜色变化。@ SunIsEngy的问题是,我不能在文本中间键入文本-这样我就不行了。