我们如何在键入的位置选择hashtag前缀单词,而不是以hashtag为前缀的其他单词,
我看起来像这样 Image Here
这里,虽然前缀的单词被突出显示,当单词后跟特殊字符或空格时,突出显示的内容结束,然后从正常文本继续
我尝试使用onTextChangedListener,但它似乎无法正常工作
message.setText("#");
message.addTextChangedListener(new TextWatcher() {
int spaces =0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String currentChar = String.valueOf(s.charAt(start));
Log.v("Character",currentChar);
// Log.v("character", String.valueOf(s)+ String.valueOf(start)+String.valueOf(before)+String.valueOf(count));
Matcher m = Pattern.compile("[^A-Za-z0-9]").matcher(currentChar);
if(!m.matches()){
Spannable spannable=new SpannableString(s);
spannable.setSpan(new StyleSpan(Typeface.BOLD), start, start, 0);
spannable.setSpan(new StyleSpan(Typeface.ITALIC), start, start, 0);
spannable.setSpan(new UnderlineSpan(), start, start, 0);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), start, start, 0);
message.setText(spannable);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});