如何SetText一些字符串重复像动画列表?

时间:2015-02-15 07:32:20

标签: java android textview

我有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

}

2 个答案:

答案 0 :(得分:2)

实现这一目标你可以:

  1. 创建一个处理程序。
  2. 使用处理程序sendMessageDelayed函数以及您想要的延迟。
  3. 覆盖handleMessage函数,并在邮件到达时更新文本视图。
  4. / *** //

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