英文:
Remove span from Editable when that part of text is changed
问题
我正在尝试对输入字段应用样式,以便当用户键入单词“congratulations”时,该单词变为粗体。我遇到的问题是,当用户开始删除文本时,剩下的字母仍然是粗体,直到它们被全部删除。
例如,如果有人键入“hi there,congratulations”,然后开始删除字母,他们会看到“hi there,congratu”而不是“hi there,congratu”。
这是我正在使用的代码:
private class StyleListener implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
s = new SpannableStringBuilder(congratStyle(s));
}
}
public static Spannable congratStyle(Spannable message) {
Pattern pattern = Pattern.compile("congratulations");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return message;
}
我尝试在while (matcher.find()) {
之前执行message = new SpannableStringBuilder(message.toString());
以清除文本中的任何样式,然后重新应用粗体,但是粗体就完全不再应用了,即结果变为“hi there,congratulations”。
理想情况下,我希望在范围内的文本发生更改时移除范围,但清除所有范围,然后在适用的地方重新应用粗体范围也是可接受的选择。
有什么想法吗?谢谢。
英文:
I'm trying to apply styles to an input field so that when a user types the word "congratulations" it becomes bold. The problem I'm having is that when the user starts deleting text, the letters left are still bold until they are all removed.
i.e. if someone typed "hi there, congratulations" and then started removing letters, they'd see "hi there, congratu" instead of "hi there, congratu".
This is the code I'm using
private class StyleListener implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
s = new SpannableStringBuilder(congratStyle(s));
}
}
public static Spannable congratStyle(Spannable message) {
Pattern pattern = Pattern.compile("congratulations");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return message;
}
I tried doing message = new SpannableStringBuilder(message.toString());
before while (matcher.find()) {
to clear the text of any spans and then re-apply the bold, but then the bold no longer gets applied at all, i.e. it results in "hi there, congratulations".
Ideally, I'd like to remove the span when the text within the span is changed, but clearing all spans and then re-applying the bold span where applicable is also an acceptable option.
Any ideas? Thanks.
答案1
得分: 0
我认为你应该将
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
改为
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_COMPOSING);
英文:
I think you should change
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
to
message.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_COMPOSING);
专注分享java语言的经验与见解,让所有开发者获益!
评论