我是Android的菜鸟,我试图从字符串的数组列表中循环文本并在文本切换器中显示它们。我希望文本每两秒更改一次。我使用this SO问题作为我的向导,用按钮切换文本没有问题。但是,当我尝试使用带有2秒延迟的for循环来循环文本时,它只显示arrayList中的第一个文本。如何使循环连续执行暂停?非常感谢任何帮助。
我的代码;
private void updateCounter()
{
try{
for (int i=0; i< CoinShowReader.tickercontent.size(); i++){
mHandler.postDelayed(new Runnable() {
public void run() {
m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));
CoinShowReader.m_counter++;
}
}, 2000);
}
}catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:4)
删除循环,你不需要它,只需在处理程序中安排anther runnable,如下所示:
void updateTextView(){
m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));
CoinShowReader.m_counter++;
mHandler.postDelayed(new Runnable() {
public void run() {
updateTextView();
} } ,2000); }
}
这样每次拨打updateTextView()
都会安排下一个电话,等等......
注意: 不要忘记插入触发器来停止该行为,因为它是无穷大