我需要在 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();
}