倒计时

作者: 资本家大恶人 | 来源:发表于2020-07-15 16:34 被阅读0次

    handler倒计时

        private int count = 5;
    
        private Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                tv_num.setText("倒计时:" + count);
                if (count == 0) {
                    Intent intent = new Intent(MainActivity.this, SplashActivity.class);
                    startActivity(intent);
                    finish();
                }
                return false;
            }
        });
    
        private Button btn_jump;
        private TextView tv_num;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            initNum();
        }
    
        private void initNum() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (count > 0) {
                        try {
                            Thread.sleep(1000);
                            count--;
                            handler.sendEmptyMessage(1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    
        private void initView() {
            btn_jump = (Button) findViewById(R.id.btn_jump);
            iv_splash = (ImageView) findViewById(R.id.iv_splash);
            tv_num = (TextView) findViewById(R.id.tv_num);
    
            btn_jump.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_jump:
                    Intent intent = new Intent(MainActivity.this, SplashActivity.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            handler.removeMessages(1);
            if (handler != null) {
                handler = null;
            }
        }
    
    

    RxJava倒计时(TianYu用不了)

        private Disposable subscribe;
    
        private void initNum() {
            //时间
            Long num = 3L;
            //period:隔几秒变一次
            subscribe = Observable.interval(1, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            if (aLong < num && !subscribe.isDisposed()) {
                                tvSplash.setText("记录改变生活" + (num - aLong - 1));
                            } else {
                                Intent intent = new Intent(MainActivity.this, WelcomActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            subscribe.dispose();
            subscribe = null;
        }
    

    相关文章

      网友评论

          本文标题:倒计时

          本文链接:https://www.haomeiwen.com/subject/jftqfktx.html