我在EditText中搜索了一些单词,经过一些逻辑处理,得到了这些单词的索引。这些索引被存储在一个ArrayList中。
然后我使用了这个函数来突出显示这些单词的颜色。
public void changeColor(EditText et, ArrayList<ArrayList<Integer>> arr) {
Spannable wordtoSpan = new SpannableString(et.getText());
if (arr==null) return;
for (int i=0; i<arr.size(); i++){
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), arr.get(i).get(0),
arr.get(i).get(1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.setText(wordtoSpan, TextView.BufferType.SPANNABLE);
System.out.println("i'm changing color now");
}
}
然而,尽管循环执行的次数与单词数量相同,只有第一个单词被高亮显示。 我尝试用TextView替换EditText,但仍只有一个单词被高亮显示。 有人可以帮忙看看这里出了什么问题吗?谢谢。