如何设置一些像Animation-List一样重复的字符串?

How to SetText Some Strings Repetitive Like Animation-List?

我有 4 个字符串,我想每 3 秒在 1 textview 中显示它们并使其重复。

喜欢显示一些 .png 文件的 animation-list

明确地说,我想这样做:

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

}

要实现这一点,您可以:

  1. 创建处理程序。
  2. 使用具有您想要的延迟的处理程序 sendMessageDelayed 函数。
  3. 覆盖 handleMessage 函数,并在消息到达时更新文本视图。

/***//

 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;
        }
    }
}
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);
    }
}