Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,它的意义我试了很久也没试出来,睡个觉,今天早上才突然有点想法,试之,果然。它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。看个截图就更明白了:
测试的Demo
public void setTextColor3() { mTextView = (TextView) findViewById(R.id.textview); // ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色 //ForegroundColorSpan不能被复用,属于一次性属性,只能给一个字段赋这样的属性 ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED); ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE); ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE); ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN); ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW); String[] array = { "word", "tell", "me", "hhe", "wo", "heihei","shuia", "ui" }; int[] score = { 90, 59, 90, 12, 22, 61, 11, 79 }; SpannableStringBuilder builder1 = new SpannableStringBuilder(""); int start = 0; int end = 0; for (int i = 0; i < array.length; i++) { start = builder1.length(); builder1.append(array[i] + " ");//添加你的字符串 end = builder1.length(); if (score[i] > 60) { builder1.setSpan(new ForegroundColorSpan(Color.GREEN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else { builder1.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } mTextView.setText(builder1); }