Countdown with textview

前端 未结 2 1064
轮回少年
轮回少年 2020-12-19 04:35

My goal is simple:

In my xml file,I have a textview called: textView2.

What I need is a countdown,that countsdown from 15 to 0,and every time a second passes

相关标签:
2条回答
  • 2020-12-19 05:19

    Try this:----

    TextView textic = (TextView) findViewById(R.id.textView2);
    
    CountDownTimer Count = new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
            textic.setText("Seconds remaining: " + millisUntilFinished / 1000);
        }
    
        public void onFinish() {
            textic.setText("Finished");
        }
    };
    
    Count.start();
    
    0 讨论(0)
  • 2020-12-19 05:33

    I think it is the matter of updating a UI element from outside the UI thread

    so try the following:

    new CountDownTimer(30000, 1000) {
    
    public void onTick(long millisUntilFinished) {
        final int j = (int) millisUntilFinished;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView textic = (TextView) findViewById(R.id.textView2);
                textic.setText(j);
            }
        });
        }
    
        public void onFinish() {        
        }
    }.start();
    
    0 讨论(0)
提交回复
热议问题