倒数计时器,以分钟和秒为单位

这是一个倒数计时器,我想在几分钟和几秒钟内打印出来。ex(300000mili)5分钟,然后数4:60、4:59 ...这是我的代码的一部分


final MyCounter timer = new MyCounter(300000,1000);

    blue.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View v) {

            timer.start();

        }

    });

}

public class MyCounter extends CountDownTimer {


    public MyCounter(long millisInFuture, long countDownInterval) {

        super(millisInFuture, countDownInterval);

    }


    @Override

    public void onFinish() {

        blue.setText("Live");

    }


    @Override

    public void onTick(long millisUntilFinished) {

        blue.setText((millisUntilFinished/60000)+":"+(millisUntilFinished/5000));

        // i tried this

    }

}

当缺少20秒时,我该如何振动?何时完成?谢谢,抱歉我的英语不好


慕姐8265434
浏览 882回答 2
2回答

森林海

http://developer.android.com/reference/android/os/CountDownTimer.htmlTextView _tv = (TextView) findViewById( R.id.textView1 );new CountDownTimer(30000, 1000) { // adjust the milli seconds here&nbsp; &nbsp; public void onTick(long millisUntilFinished) {&nbsp; &nbsp; _tv.setText(""+String.format("%d min, %d sec",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));&nbsp; &nbsp; }&nbsp; &nbsp; public void onFinish() {&nbsp; &nbsp; &nbsp; &nbsp;_tv.setText("done!");&nbsp; &nbsp; }&nbsp;}.start();从Java 1.5开始,存在java.util.concurrent.TimeUnit类&nbsp;_tv.setText(""+String.format("%d min, %d sec",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.htmlTimeUnit表示给定粒度单位的持续时间,并提供实用方法来跨单位转换以及在这些单位中执行计时和延迟操作。对于1.5以下的Java版本或不完全支持TimeUnit类的系统,可以使用以下公式:int seconds = (int) (milliseconds / 1000) % 60 ;int minutes = (int) ((milliseconds / (1000*60)) % 60);int hours&nbsp; &nbsp;= (int) ((milliseconds / (1000*60*60)) % 24);振动&nbsp;Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Vibrate for 500 milliseconds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.vibrate(500);&nbsp; <uses-permission android:name="android.permission.VIBRATE"/> // permission in manifest编辑:在4分55秒振动500毫秒&nbsp;if((TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)==4) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==55)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Vibrate for 500 milliseconds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.vibrate(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
Android