倒计时

private int time=5;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==10) {
                if (time > 0) {
                    time--;
                    text_time.setText(time + "s");
                    handler.sendEmptyMessageDelayed(10, 1000);
                }else{
                    Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                }
            }
```//发送一个handler
        handler.sendEmptyMessageDelayed(10,1000);
        //点击监听
        text_tiao.setOnClickListener(new View.OnClickListener() {
         
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(null);
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
        //添加动画
        Animation set = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set_layout);
        relat_layout.startAnimation(set);
    }

你可能感兴趣的:(倒计时)