Android中實現倒計時的幾種方式


1.使用 CountDownTimer 實現倒計時

/**
* CountDownTimer timer = new CountDownTimer(3000, 1000)中,
* 第一個參數表示總時間,第二個參數表示間隔時間。
* 意思就是每隔一秒會回調一次方法onTick,然后1秒之后會回調onFinish方法。
*/ 
CountDownTimer timer = new CountDownTimer(3000, 1000) { public void onTick(long millisUntilFinished) { txt.setText("倒計時" + millisUntilFinished / 1000 + "秒"); } public void onFinish() { Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } }; //調用 CountDownTimer 對象的 start() 方法開始倒計時,也不涉及到線程處理 timer.start();

 

2.利用Handler實現倒計時

 

private int count = 5;
    private TextView tv;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==100){
                if(count>0){
                    tv.setText(count+"");
                    count--;
                    handler.sendEmptyMessageDelayed(100,1000);
                }else {
                    Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(intent);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
        handler.sendEmptyMessageDelayed(100,1000);
    }

 

3.利用動畫實現倒計時

private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        ValueAnimator animator = ValueAnimator.ofInt(5,0);
        //設置時間
        animator.setDuration(5000);
        //均勻顯示
        animator.setInterpolator(new LinearInterpolator());
        //監聽
        animator.addUpdateListener(new AnimatorUpdateListener() {
            
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                tv.setText(value+"");
                if(value==0){
                    startActivity(new Intent(MainActivity.this,Second.class));
                }
            }
        });
        animator.start();
    }

 

4.利用Timer定時器

 

private TextView tv;    
    static int count = 5;

    Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            int count = msg.arg1;
            if(count==0){
                iv.setText("你好");
            }else {
                iv.setText(count+"");
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.iv);
        
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            
            @Override
            public void run() {
                Message message = new Message();
                message.arg1 = count;
                if(count!=-1){
                    count--;
                }else {
                    return;
                }
                handler.sendMessage(message);
            }
        }, 1000,1000);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM