如何在倒数计时器中传递秒数值,我能够通过分钟而不是秒,它始终从60开始

时间:2016-01-23 05:52:16

标签: android timer

这是我开始的方法,我从Textview传递分钟,如何将用户定义的秒数传递给星星

   startbtnn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                final CounterClass timer = new CounterClass(Integer.parseInt(timertxt11.getText().toString())*60000,1000,v);

                timer.start();
            }
        });

这是使用的类

       public class CounterClass extends CountDownTimer
        {

        public CounterClass(long millisInFuture, long countDownInterval,View v)
        { super(millisInFuture, countDownInterval);
        }


        @Override
        public void onFinish()
        {
//            timertxt.setText("Completed.");
        }

         @Override
        public void onTick(long millisUntilFinished)
        {
            long millis = millisUntilFinished;
            String hms1 = String.format("%02d",TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)));
            String hms2 = String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));


            System.out.println(hms1+": " +hms2);
            timertxt11.setText(hms1);
            timertxt22.setText(hms2);
        }
    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果您有两个TextView(一个显示分钟,另一个显示秒)

int mins = Integer.parseInt(minTextView.getText().toString());
int secs = Integer.parseInt(secTextView.getText().toString());
long totalMillis = (mins * 60 + secs) * 1000L;
new CounterClass(totalMillis, 1000L, v).start();

或者如果你只想要一个TextView

try {
    // "mm:ss" is just an example. define the input format yourself.
    SimpleDateFormat format = new SimpleDateFormat("mm:ss", Locale.getDefault());
    Date date = format.parse(timeTextView.getText().toString());
    new CounterClass(date.getTime(), 1000, v).start();
} catch (ParseException e) {
    // handle exception here
}