如何在for循环中使用SpannableStringBuilder?

时间:2019-06-04 11:18:33

标签: android

这是我的代码

        for (int i = 0; i < StationNMArray.size(); i++) {
            String current_Station = StationNMArray.get(i);
            station_in_course = station_in_course + "\nstname: " + current_Station
                    + "\ndate: " + year + "year " +  month + "month " +  day + "day"
                    + "\ntime: " + currentHourArray.get(i) + "hour " + currentMinArray.get(i) + "minute"
                    + "\npredict: " + currentPredict.get(i) + "\n";


        }

我希望predict: + current.get(i)更改颜色

所以我尝试了

        for (int i = 0; i < StationNMArray.size(); i++) {
            String current_Station = StationNMArray.get(i);
            station_in_course = station_in_course + "\nstbane: " + current_Station
                    + "\ndate: " + year + "year " +  month + "month " +  day + "day"
                    + "\ntime: " + currentHourArray.get(i) + "hour " + currentMinArray.get(i) + "minute"
                    + "\npredict: " + currentPredict.get(i) + "\n";
            ssb = new SpannableStringBuilder(station_in_course);
            String word = "predict: ";
            int start = station_in_course.indexOf(word);
            int end = start + word.length()+3;
            ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#FF6702")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.setSpan(new RelativeSizeSpan(1.3f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}

但是此代码不起作用

我该如何解决?

3 个答案:

答案 0 :(得分:0)

我认为问题是我从您的问题中得到的是,您正在以字符串格式而不是可扩展的字符串生成器保存您的数据。

您需要将实现更改为:

SpannableStringBuilder station_in_course = new SpannableStringBuilder();

for (int i = 0; i < StationNMArray.size(); i++) {
            String current_Station = StationNMArray.get(i);
            String text = "\nstbane: " + current_Station
                    + "\ndate: " + year + "year " +  month + "month " +  day + "day"
                    + "\ntime: " + currentHourArray.get(i) + "hour " + currentMinArray.get(i) + "minute"
                    + "\npredict: " + currentPredict.get(i) + "\n";
            ssb = new SpannableStringBuilder(text);
            String word = "predict: ";
            int start = station_in_course.indexOf(word);
            int end = start + word.length()+3;
            ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#FF6702")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.setSpan(new RelativeSizeSpan(1.3f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            station_in_course.append(ssb);
}

答案 1 :(得分:0)

您需要这样更改实现:

final Pattern ptn = Pattern.compile("android"); 
// i have 9 "android" in my string
final Matcher matcher = ptn.matcher(getString(R.string.test2));
final SpannableStringBuilder spannable = new 
SpannableStringBuilder(getString(R.string.test));

while (matcher.find()) {
    final ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
    spannable.setSpan(span, matcher.start(), matcher.end(), 
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
txtView.setText(spannable);

答案 2 :(得分:0)

        SpannableStringBuilder ssb;
        for (int i = 0; i < StationNMArray.size(); i++) {
            String current_Station = StationNMArray.get(i);
            station_in_course = station_in_course + "\n역명: " + current_Station
                    + "\n날짜: " + year + "년 " + month + "월 " + day + "일"
                    + "\n시간: " + currentHourArray.get(i) + "시 " + currentMinArray.get(i) + "분"
                    + "\n포화도: " + Math.round(currentPredict.get(i)) + "\n";

        }
        ssb = new SpannableStringBuilder(station_in_course);
        String word = ssb.toString();
        String find = "포화도: ";
        for (int index = word.indexOf(find); index >= 0; index = word.indexOf(find, index + 1)) {

            int start = index;
            int end = start + find.length() + 3;

            String t2 = word.substring(end-3, end-1);
            int t = Integer.parseInt(t2);
            System.out.println(t);

            if (t >0 && t <=50) {
                ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#008000")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new RelativeSizeSpan(1.5f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            else if(t >50 && t <=100)
            {
                ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#ffcc66")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new RelativeSizeSpan(1.5f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            else if(t>100 && t < 150)
            {
                ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#fdff00")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new RelativeSizeSpan(1.5f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            else if(t < 200)
            {
                ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new RelativeSizeSpan(1.5f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            else
            {
                ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#0000ff")), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.setSpan(new RelativeSizeSpan(1.0f), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }