Handler倒计时

来源:互联网 发布:dns默认端口 编辑:程序博客网 时间:2024/05/17 22:40
package com.example.daojishi;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
ImageView iv;
AnimationDrawable ad;
TextView tv;
int time=5;
Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv=(ImageView) findViewById(R.id.iv);
        tv=(TextView) findViewById(R.id.tv);
        ad=(AnimationDrawable) iv.getBackground();
        ad.start();
        tv.setText("倒计时:"+time);
        handler.postDelayed(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                time--;
                handler.postDelayed(this, 1000);
                if(time==-1){
                    Intent intent=new Intent(MainActivity.this, OtherActivity.class);
                    startActivity(intent);
                }else{
                        tv.setText("倒计时:"+time);
                }
                
            }
        }, 1000);
    }
    @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            handler.removeCallbacksAndMessages(null);
        }
}

原创粉丝点击