我需要在TextView中显示数组中的许多字符串值。我使用Runnable。但它只运行一次!如果我使用for循环,代码将抛出错误。
Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
label.setText(rates[nCounter]);
}
};
try {
handler.removeCallbacks(hMyTimeTask);
handler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
e.printStackTrace();
}
使用for循环:
Runnable hMyTimeTask = new Runnable() {
public void run() {
for (int i = 0; i < rates.length; i++) {
label.setText(rates[i]);
}
}
};
try {
handler.removeCallbacks(hMyTimeTask);
handler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
e.printStackTrace();
}
我使用runnable。但它只运行一次!
在你的Runnable中,你需要再次发布你的Runnable来重复它:
Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
if(nCounter >= rates.length)
nCounter = 0;
label.setText(rates[nCounter]);
// Run this again
handler.postDelayed(this, 1000); // delay 1 second
}
};