TextSwitcherTest

来源:互联网 发布:软件0x0000005 编辑:程序博客网 时间:2024/06/06 10:41
 
package hyz.com;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.util.Log;import android.view.View;  import android.view.animation.AnimationUtils;   import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher.ViewFactory;public class TextSwitcherTest extends Activity implements ViewFactory {    private TextSwitcher switcher;      private int id= 0;     private String [] resources={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};      @Override      public void onCreate(Bundle savedInstanceState)     {          Log.e("hyz","onCreate");        super.onCreate(savedInstanceState);          setContentView(R.layout.main);          switcher = (TextSwitcher) findViewById(R.id.switcher);          switcher.setFactory(this);          switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));          switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));                  Timer timer = new Timer();               timer.scheduleAtFixedRate(new MyTask(), 10000, 3000);//程序执行到此条语句,开始等待10秒,然后每隔3秒执行new MyTask()一次。     }      @Override      public View makeView()     {          Log.e("hyz","makeView");        TextView tv =new TextView(this);          tv.setText(resources[id]);          return tv;      }     private Handler mHandler = new Handler()     {                          public void handleMessage(Message msg)             {                    switch (msg.what)                {                    case 1:                        id = (id+1)%(resources.length);                    switcher.setText(resources[id]);                       break;                    }                };         };        private class MyTask extends TimerTask     {                @Override                public void run()             {                 Log.e("hyz","MyTask");                Message message = new Message();                    message.what = 1;                    mHandler.sendMessage(message);                                  }            }}