Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
// TODO Auto-generated method stub
Log.i("first iteration","first iteration");
btn1.setTextColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
Log.i("iterating","iteratinggggggggg");
}
}, 0, 1000);
Logcat で:
01-07 02:39:09.789: I/first iteration(16568): first iteration
01-07 02:39:09.789: I/iterating(16568): iteratinggggggggg
01-07 02:39:10.781: I/first iteration(16568): first iteration
つまり、一度だけbtn1.setTextColor(...)
実行されます! Button Textを1 秒ごとに変更したいと思います。
どんな専門家も助けてくれますか?
Ole のおかげで、あなたと共有したい問題の解決策を見つけることができました。
解決:
// UPDATING BTN TEXT DYNAMICALLY
Runnable myRunnableUpdater = new Runnable()
{
public void run() {
colorGenerator();
hd.postDelayed(myRunnableUpdater, 1000);
}
};
void startRepeatingTask()
{
myRunnableUpdater.run();
}
void stopRepeatingTask()
{
hd.removeCallbacks(myRunnableUpdater);
}
private void colorGenerator() {
btn1.setTextColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
}
//END OF UPDATING BTN TEXT DYNAMICALLY!!
1)宣言することを忘れないでくださいHandler hd
2)また、hd = new Handler()
3 onCreate()
)startRepeatingTask()
繰り返しコードを繰り返したい場所で使用してください。
4)stopRepeatingTask()
繰り返すのをやめたいところならどこでも使用できます。
乾杯!;)