【发布时间】:2013-08-03 19:46:21
【问题描述】:
问题
我有将近 20 个TextView,我需要做一些事情来让它们的背景颜色动态变化。
例如:所有TextView 具有相同的默认背景颜色,然后在 5 秒后第一个将变为红色,而其他人仍然相同,然后在再过 5 秒后第一个 TextView 的背景颜色将变为默认值,第二个 @987654325 @ 的背景颜色会变成红色,并且会这样继续。
我试图将它们放入线程和处理程序的 for 循环中,但它们一起改变了。我该怎么办?
我试过这个代码
Thread color=new Thread() {
public void run() {
for(int i=2131230724;i<=2131230742;i++){
TextView currentText = (TextView) findViewById(i);
currentText.setBackgroundColor(Color.RED);
try {
sleep(2000);
currentText.setBackgroundColor(Color.TRANSPARENT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
然后我用了
mHandler.post(color);
解决方案
我找到了解决我的问题的方法,如果其他人也需要,请分享。
Runnable myhandler=new Runnable() {
int id=2131230724;//First textView id
@Override
public void run() {
// TODO Auto-generated method stub
if(id==2131230724){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime1);
currentText.setBackgroundColor(Color.RED);
id++;
}
else if(id==2131230725){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime2);
currentText.setBackgroundColor(Color.RED);
TextView PreText=(TextView)findViewById(R.id.egzersiz01_kelime1);
PreText.setBackgroundColor(Color.TRANSPARENT);
id++;
}//And all other id's to else if as same as mine.
mHandler.postDelayed(myhandler, delay);
在 onCreate() 中
myhandler.run();
我曾尝试使用 for 循环来简化它,但它崩溃了,我不得不将它们全部写出来。 感谢您的帮助...
【问题讨论】:
-
发布您当前拥有的代码,我们可以帮助您调整它。
标签: android loops handler ui-thread dynamic-ui