我的 Spannable 不会改变重复字符的颜色
My Spannable won't change color of repetitive characters
我的 Spannable 不会更改重复字符的颜色。例如,我有两个相同的字符“$”美元符号,第一个更改为蓝色,但第二个不是。我正在输入我的文字不会改变的是字符串数组。当我调试时,我可以在适当的索引上看到其中的所有文本。
String secondRowString = "Based on your current monthly savings of" + " $" + FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
+ " at a rate of return of " + FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()) + "% "
+ " , you are on track to have " + "$" + FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
+ " in total savings by " + model.getRetirementDdata().getRetirementMembersList().get(0).getName()
+ " age " + model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement();
SpannableString strThirdRow = new SpannableString(secondRowString);
SpannableTextUtils.setColorForPath(strThirdRow, new String[]{"$"
, FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
, FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()), "%", "$"
, FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
, String.valueOf(model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement())}, ContextCompat.getColor(context, R.color.debt_payments));
binding.secondTV.setText(strThirdRow);
和我的 SpannableString.setColorsForPath()
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
if (indexOfPath == -1) {
continue;
}
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
这是您代码中的逻辑错误 indexOf
每次都会 return 第一次出现。虽然您需要在 String 中找到所有出现的单词。
这可以通过多种方式完成。基本上你需要解决问题 Find Indexes of all occurrences of a word in a string
.
您可以使用以下方法。
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
while(indexOfPath >= 0) {
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
indexOfPath = spannable.toString().indexOf(path, indexOfPath+1);
}
}
}
我的 Spannable 不会更改重复字符的颜色。例如,我有两个相同的字符“$”美元符号,第一个更改为蓝色,但第二个不是。我正在输入我的文字不会改变的是字符串数组。当我调试时,我可以在适当的索引上看到其中的所有文本。
String secondRowString = "Based on your current monthly savings of" + " $" + FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
+ " at a rate of return of " + FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()) + "% "
+ " , you are on track to have " + "$" + FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
+ " in total savings by " + model.getRetirementDdata().getRetirementMembersList().get(0).getName()
+ " age " + model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement();
SpannableString strThirdRow = new SpannableString(secondRowString);
SpannableTextUtils.setColorForPath(strThirdRow, new String[]{"$"
, FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
, FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()), "%", "$"
, FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
, String.valueOf(model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement())}, ContextCompat.getColor(context, R.color.debt_payments));
binding.secondTV.setText(strThirdRow);
和我的 SpannableString.setColorsForPath()
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
if (indexOfPath == -1) {
continue;
}
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
这是您代码中的逻辑错误 indexOf
每次都会 return 第一次出现。虽然您需要在 String 中找到所有出现的单词。
这可以通过多种方式完成。基本上你需要解决问题 Find Indexes of all occurrences of a word in a string
.
您可以使用以下方法。
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
while(indexOfPath >= 0) {
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
indexOfPath = spannable.toString().indexOf(path, indexOfPath+1);
}
}
}