Android 突出显示字符串中的子字符串

Android 突出显示字符串中的子字符串,android,spannablestring,Android,Spannablestring,我想突出显示一个字符串中的单个单词,然后在一段随机时间后,我想更改下一个单词的颜色 我知道如何更改子字符串的颜色,但我想在运行时这样做,并不断更改下一个单词的颜色 请帮忙 `@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub String coloredString; String myStr

我想突出显示一个字符串中的单个单词,然后在一段随机时间后,我想更改下一个单词的颜色

我知道如何更改子字符串的颜色,但我想在运行时这样做,并不断更改下一个单词的颜色

请帮忙

`@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    String coloredString;
    String myStr[] = myStrList.get(position).split("-");

    for(int i=0; i<myStr.length;i++){

         if(position==selectionRowIndex){
             if(i==selectionWordIndex){
                 String colorWord = "<font color='#EE0000'>"+myStr[i]+"</font> ";
                 coloredString+=colorWord;
             }else{
                 coloredString+=myStr[i];
             } 
         }else{
             coloredString+=myStr[i];
         }

    }

     ViewHolder holder = null;
     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

     if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();


            holder.txtStr = (TextView) convertView.findViewById(R.id.text_Str);


            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();




            holder.txtStr .setText(Html.fromHtml(coloredString));

创建一个Spannable对象,通过SpannableString设置其文本

Spannable spanobj = new SpannableString("word to set color");        
spanobj.setSpan(new ForegroundColorSpan(Color.WHATEVER), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
您可以使用此选项设置textview.setTextspanobj上的文本

循环0、4开始索引、结束索引,比如说5、7,并更新视图,使更多的字符串着色。 0,4将设置“word”的颜色
5,7应将颜色设置为等…

请参阅ForeGroundColorSpan请发布您的代码。到目前为止你做了什么?您的问题目前缺乏足够的信息来诊断问题,可能会被关闭。我希望它能始终如一地移动。我的字符串在listview中。第一个单词是红色,另一个是黑色,1秒钟后第二个单词是红色,另一个是黑色,依此类推,我将如何计算字符串中单词的开始和结束索引这是一个单独的问题,但你可以将字符串拆分为空格,然后迭代生成的数组,得到indexofsplitaray[I]及其长度。然后,您的值是index&index+length。这并不能解释重复的单词,但我相信你能想出一个方法来做到这一点。