我的可分割件不会改变重复字符的颜色

My Spannable won't change color of repetitive characters

提问人:gojic 提问时间:3/26/2021 更新时间:3/26/2021 访问量:43

问:

我的 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);
    }
}
java android spannablestring

评论


答:

1赞 ADM 3/26/2021 #1

它在你代码中的一个逻辑错误将返回第一次出现每次。虽然您需要在 String 中找到单词的所有出现情况。 这可以通过多种方式完成。基本上你需要解决问题。indexOfFind 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);
        }
    }
}