我正在编写一个应用程序来读取一行文本,并在读取行时连续突出显示每个单词。这个想法是开始玩线(这是一本儿童图画书,一次一行),然后阅读文本,以毫秒为单位的每个单词的长度,然后在正确的时间突出显示textview中的单词。连续延迟的android处理程序
我的方法是: 将句子的单词放入一个数组中(并且最终每个作品的长度,但是暂时假设每个都为1000ms); 将单词写入textViewPartial; 单词的延迟长度; 向句子添加下一个单词并将其写入textViewPartial ....等。
但我不能计算出时间。阅读我在处理程序和异步中可以找到的所有内容,最好的我可以提出如下 - 我在for循环中放置了一个postdelayed处理程序。我的大脑说它每次循环都会延迟,但是你可以从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
第一个问题 - 这是摆在首位的正确方法? 第二个问题 - 我怎样才能使它工作?
非常感谢。
谢谢你的工作。但是你是否暗示我需要自己清理runnables?我可以看到这将是一个好习惯。我将不得不研究这一点。 – dulciepercy
我不太清楚,但我不相信如果你的Activity停止,你的runnables会自动清理。如果我使用你的应用程序,如果离开你的应用程序,我会很生气,而你的书一直在后台阅读!最好检查文档以进行验证。 – MJD