文本视图中的倒数计时器

countdown timer in text view

i wrote this code and i want when i click on btnGetPincode, a 60 sec count down timer start to run. but it didnt happen and the result in textview= 00:00 and nothing happen. why?

这是我的代码:

 btnGetPinCode.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
 btnGetPinCode.setClickable(false);
                btnGetPinCode.setBackgroundResource((R.drawable.button4));
                txtShowPinCode.setVisibility(View.VISIBLE);

                //initialize timer duration
                long duration = TimeUnit.MINUTES.toMillis(1);

                //initialize timer countdown timer
                new CountDownTimer(duration, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
                                , TimeUnit.MILLISECONDS.toMinutes(1)
                                , TimeUnit.MILLISECONDS.toSeconds(1) -
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(1)));

                        //set converted time to textView
                        txtTimer.setVisibility(View.VISIBLE);
                        txtTimer.setText(duration2+"");
                        btnOk.setText(duration2);
                    }

                    @Override
                    public void onFinish() {
                        //when timer finished, hide text view
                        txtTimer.setVisibility(View.INVISIBLE);
                        btnGetPinCode.setBackgroundResource(R.drawable.button);
                        btnGetPinCode.setClickable(true);
                    }
                }.start();
    });

}

问题:您在 duration2 变量的每个参数中设置了 1,因此 TextView 永远不会更改,您需要设置在每个计时器滴答时更改的实际值是onTick()方法的参数>> millisUntilFinished

将您的计时器更改为:

new CountDownTimer(duration, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
                , TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) //<<<<< change here
                , TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - //<<<< change here
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); //<<<<< change here

        //set converted time to textView
        txtTimer.setVisibility(View.VISIBLE);
        txtTimer.setText(duration2+"");
        btnOk.setText(duration2);
    }

    @Override
    public void onFinish() {
        //when timer finished, hide text view
        txtTimer.setVisibility(View.INVISIBLE);
        btnGetPinCode.setBackgroundResource(R.drawable.button);
        btnGetPinCode.setClickable(true);
    }
}.start();

主要问题在于时间的计算duration2

//Declare timer
CountDownTimer cTimer = null;

cTimer  = new CountDownTimer(duration, 1000) {
@Override
public void onTick(long millisUntilFinished) {
    String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
            , TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) //Here is the problem
            , TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - //Here is the problem
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); //<<<<< change here

    //set converted time to textView
    txtTimer.setVisibility(View.VISIBLE);
    txtTimer.setText(duration2+"");
    btnOk.setText(duration2);
}

@Override
public void onFinish() {
    //when timer finished, hide text view
    txtTimer.setVisibility(View.INVISIBLE);
    btnGetPinCode.setBackgroundResource(R.drawable.button);
    btnGetPinCode.setClickable(true);
 }
}.start();

您还应该需要清理引用以防止内存泄漏。每当调用拥有 Activity/Fragment 中的 onDestroy()/onDestroyView() 时,都需要调用 cTtimer.cancel()。

 void cancelTimer() {
  if(cTimer!=null)
    cTimer.cancel();
  }