我想在"之间对文字进行着色。和"。 例如 -
<
body bgcolor = &#34;#000000&#34; >
(我无法在问题中为它们着色,所以我只是制作了它们粗体)
我正在使用以下代码,但它无法正常工作 -
if (text.contains("\"")) {
String syntaxText = "\"";
SparseIntArray array = new SparseIntArray();
int ofe = text.indexOf(syntaxText, 0);
for (int ofs = 0; ofs < text.length() && ofe != -1; ofs = ofe + 1) {
ofe = text.indexOf(syntaxText, ofs);
if (ofe == -1) {
break;
} else {
for (int i = 0; i < text.length(); i=i+2) {
array.put(i, ofe);
}
}
}
int size = array.size();
Spannable WordtoSpan = new SpannableString(et_note.getText());
for (int i = 0; i < size; i=i++) {
WordtoSpan.setSpan(new ForegroundColorSpan(0xFF00FF00), array.get(i), array.get(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et_note.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
虽然,我并不期望上面的代码能够正常工作,因为我有点困惑。
谢谢你的帮助。
编辑 -
我的代码中的et_note
是一个EditText。该文本将由当时用户提供,并且可能包含许多&#34; 。
答案 0 :(得分:0)
我们假设startPos, endPos
是两个"
的张贴。请尝试以下代码:
if (startPos != -1){
Spannable spannable = new SpannableString(textValue);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
else{
textView.setText(textValue);
}