我有4个字符串,我想每3秒在textview
显示一次,并重复它。
与显示animation-list
文件的.png
类似。
要明确,我想这样做:
while(true){
tv.SetText("Text1");
//delay for 3 second
tv.SetText("Text2");
//delay for 3 second
tv.SetText("Text3");
//delay for 3 second
tv.SetText("Text4");
//delay for 3 second
}
答案 0 :(得分:2)
实现这一目标你可以:
/ *** //
private static final int MSG_UPDATE_STRING = 1;
private static final int STRING_REFRESH_INTERVAL_MILLIS = 3000;
private static StaticHandler mHandler = new StaticHandler();
//StaticHandler is an inner class. write it inside your activity class.
public static class StaticHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_STRING:
//update textview here
...
//resend message so it will continue to refresh
mHandler.sendEmptyMessageDelayed(MSG_DATA_PACKET_TIMEOUT,STRING_REFRESH_INTERVAL_MILLIS );
break;
}
}
}
答案 1 :(得分:1)
public class MainActivity extends ActionBarActivity {
private TextView textView;
private int count = 1;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText(count+"");
count++;
if (count > 3) {
handler.removeCallbacks(this);
} else {
handler.postDelayed(this, 3000);
}
}
}, 0);
}
}