我正在编写一个应用程序来读取一行文本,并在读取该行时连续突出显示每个单词。这个想法是开始播放这行(这是一本儿童图画书,所以一次一行),然后阅读文本,每个单词的长度(以毫秒为单位),然后在正确的时间在 textview 中突出显示该单词。
我的方法是:将句子的单词放入一个数组中(最后是每个作品的长度,但目前只假设每个 1000 毫秒);将单词写入 textViewPartial;字的延迟长度;将下一个单词添加到句子中并将其写入 textViewPartial....等。
但我无法计算出时间。阅读我在处理程序和异步上可以找到的所有内容,我能想到的最好的方法如下 - 我将一个后延迟处理程序放在一个 for 循环中。我的大脑说每次循环循环时它都会延迟,但你可以从 logcat 输出中看到它没有。在 for 循环开始之前只有一个延迟。这是我的第一篇文章,我看不到你们是如何从 Eclipse 中获取彩色代码的,所以我希望它看起来没问题。
   public class LineOutHanler extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_out_hanler);
    TextView t = new TextView(this);
    t=(TextView) findViewById (R.id.textView10);
    String textOut = "Oh how my spleen aches to see you again";
    final TextView textViewPartial = (TextView) findViewById(R.id.textView11);
    final String[] wordsOut = textOut.split(" ");
    final int wordsInSentence = wordsOut.length;
    int[] wordLength = new int[wordsInSentence];
            for (int counter=0;counter<=wordsInSentence-1;counter++){
            wordLength[counter]=wordsOut[counter].length();}
    String partialSentence ="";
    for (int counter=0; counter<=wordsInSentence-1; counter++){   
            String  c= addWordsOut(wordsOut[counter], partialSentence);
            textViewPartial.setText(c);
            partialSentence = c;
            Log.d("Word", partialSentence);
    final String partialOut=c;
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
                        textViewPartial.setText(partialOut);
            Log.d("Handler", partialOut);
        }
    } , 2000);}
    }
public String addWordsOut (String part, String upToHere)  {
upToHere=upToHere+" " + part;
return upToHere;
}
}
和 logcat 输出:
10-19 23:07:32.248: E/cutils-trace(39): Error opening trace file: No such file or directory (2)
10-19 23:07:32.368: D/AudioSink(39): bufferCount (4) is too small and increased to 12
10-19 23:07:32.379: D/Word(821):  Oh
10-19 23:07:32.379: D/Word(821):  Oh how
10-19 23:07:32.388: D/Word(821):  Oh how my
10-19 23:07:32.388: D/Word(821):  Oh how my spleen
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches to
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches to see
10-19 23:07:32.398: D/Word(821):  Oh how my spleen aches to see you
10-19 23:07:32.398: D/Word(821):  Oh how my spleen aches to see you again
10-19 23:07:33.328: I/Choreographer(288): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-19 23:07:33.368: I/ActivityManager(288): Displayed com.example.testtextout/.LineOutHanler: +1s820ms
10-19 23:07:35.320: W/AudioFlinger(39): write blocked for 1091 msecs, 1 delayed writes, thread 0x40e0b008
10-19 23:07:35.320: D/Handler(821):  Oh
10-19 23:07:35.329: D/Handler(821):  Oh how
10-19 23:07:35.329: D/Handler(821):  Oh how my
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen aches
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen aches to
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see you
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see you again
10-19 23:08:30.588: D/dalvikvm(396): GC_FOR_ALLOC freed 452K, 15% free 3047K/3556K, paused 40ms, total 65ms
10-19 23:25:42.149: D/dalvikvm(288): GC_FOR_ALLOC freed 850K, 31% free 5593K/7996K, paused 99ms, total 117ms
第一个问题——这首先是正确的方法吗?第二个问题 - 我怎样才能让它工作?
非常感谢。