Android:尝试使用 Spannable 更改字符串的颜色(部分有效)
Android: Trying to change color from string using Spannable (partially working)
我有一个名为 note 的字符串。我将这段代码用于更改两个 * * 字符之间特定单词的颜色。它部分工作正常。我面临的问题是在我插入这个符号“➥”之后,没有将 * * 之间的确切单词着色为黄色(短语示例:This is a *good*
book - 它在单词开始前的最后一个字母变为黄色使用 * - a good
而不是 good
)。我需要协助。我是新手
if(note != null){
int firstIndex = note.indexOf("*");
if (firstIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
int secIndex = note.indexOf("*");
if (secIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
String newString = "➥ "+note;
Spannable spannable = new SpannableString(newString);
spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtnote.setText(spannable);
}
}
}
原因是字符串的长度已在 2 个符号上更改,因此索引也已更改。
对我来说这不是个好方法,但作为快速解决方案:
int removedSymbolCount = 0;
if (firstIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
removedSymbolCount++;
int secIndex = note.indexOf("*");
if (secIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
removedSymbolCount++;
String newString = "➥ "+note;
Spannable spannable = new SpannableString(newString);
spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), firstIndex + removedSymbolCount, secIndex + removedSymbolCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), firstIndex + removedSymbolCount, secIndex + removedSymbolCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtnote.setText(spannable);
}
}
我认为可以使用 Matcher 和 regex expressions.
以获得更好的解决方案
我有一个名为 note 的字符串。我将这段代码用于更改两个 * * 字符之间特定单词的颜色。它部分工作正常。我面临的问题是在我插入这个符号“➥”之后,没有将 * * 之间的确切单词着色为黄色(短语示例:This is a *good*
book - 它在单词开始前的最后一个字母变为黄色使用 * - a good
而不是 good
)。我需要协助。我是新手
if(note != null){
int firstIndex = note.indexOf("*");
if (firstIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
int secIndex = note.indexOf("*");
if (secIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
String newString = "➥ "+note;
Spannable spannable = new SpannableString(newString);
spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtnote.setText(spannable);
}
}
}
原因是字符串的长度已在 2 个符号上更改,因此索引也已更改。
对我来说这不是个好方法,但作为快速解决方案:
int removedSymbolCount = 0;
if (firstIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
removedSymbolCount++;
int secIndex = note.indexOf("*");
if (secIndex >= 0) {
note = note.replaceFirst("[*]{1}", "");
removedSymbolCount++;
String newString = "➥ "+note;
Spannable spannable = new SpannableString(newString);
spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), firstIndex + removedSymbolCount, secIndex + removedSymbolCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), firstIndex + removedSymbolCount, secIndex + removedSymbolCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtnote.setText(spannable);
}
}
我认为可以使用 Matcher 和 regex expressions.
以获得更好的解决方案