目前我在处理我的Android应用程序时遇到了问题。
为了解释:
首先,我想逐个字符地在TextView中显示文本。这是我当前的代码:
tvIntro.setText("");
final Handler textHandler = new Handler();
for(int i=0; i<intro.length();i++){
final int finalCount = i;
textHandler.postDelayed(new Runnable() {
@Override
public void run() {
tvIntro.setText(tvIntro.getText() + (intro.charAt(finalCount)+""));
}
}, 150 * i);
}
在整个文本显示完毕后,我希望播放声音并持续5秒钟改变屏幕的颜色。为此,我的代码如下:
myBackground.setBackgroundColor(Color.RED);// set initial colour
final Thread blink = new Thread(new Runnable() {
public void run() {
while (getRunning()) {
try {
Thread.sleep(100);
if(start[0] !=1){
mp.start();
start[0] = 1;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
updateColor(myBackground);
whichColor = !whichColor;
}
}
});
private void updateColor(final RelativeLayout myBackground) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (whichColor)
myBackground.setBackgroundColor(Color.RED);
else
myBackground.setBackgroundColor(Color.GREEN);
}
});
}
所有功能都在工作,但我想在执行第二个处理程序之前完成第一个处理程序。此外,第二个处理程序应该在x秒后停止。
我有一些问题不理解处理程序和线程的工作原理。如果你们中的某个人有解决方案,那就太好了。