我有一個名爲nota的字符串。有一些文字。我想要的是從包含* *之間的短語中更改特定單詞的顏色。例如:*真棒*。 awesome
這個詞應該是黃色,* *字符應該被刪除(就像*是一個迷你Markdown格式)。我試過這個代碼,但它不起作用。我需要一些幫助。Android試圖替換字符並更改字詞的顏色
if(nota.contains("* *")){
//nota = nota.replace("* *","");
Pattern pattern = Pattern.compile("\\*.*\\*");
Matcher matcher = pattern.matcher(nota);
// Check all occurrences
matcher.find();
Spannable spannable = new SpannableString(nota);
spannable.setSpan(
//new ForegroundColorSpan(Color.YELLOW), nota.indexOf("* *"), nota.indexOf("* *") + "* *".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
new ForegroundColorSpan(Color.YELLOW), matcher.start() + 1 , matcher.end() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtnota.setText(spannable);
}
我想你的代碼。我遵循你的步驟,並把'*真棒*',但沒有任何改變。 astherics是可見的,沒有黃色插入真棒。 –