Android: Chronometer SetBase in minutes

浪子不回头ぞ 提交于 2019-12-11 19:15:23

问题


Is there a way by which using the android chronometer class to set base of the chronometer in 15 minutes and from that period the times goes down until 0 seconds?

I have tried with setBase(60000) but this isn't work.


回答1:


In general the chronometer works like this (if you would like to set the Base to a specific nr):

mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))

what you are asking can be done through a countdown (http://developer.android.com/reference/android/os/CountDownTimer.html)

Or create your own countdown by using the chronometer like this (more work should be done cause i just wrote this and did not test it yet)

private OnChronometerTickListener countUp = new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer){
            long elapsedTime = (SystemClock.elapsedRealtime() - mChronometerCountUp.getBase()) / 60000; 
            Log.v("counting up", elapsedTime); 
            // you will see the time counting up
            count_down--; 
                if(count_down == 0){
                mChronometerCountUp.stop();
            }
            // an int which will count down, 
            // this is not (very) accurate due to the fact that u r using the update part of the chronometer 
            // u just might implement the countdown i guess 
            // or 2 chronometers (one counting up and an other counting down using the elapsed time :p)
            // just remember programming is creating ur solution to problems u face its like expression urself
        };
    };



回答2:


Check out this thread Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"? as well as this thread Android - Get time of chronometer widget. Neither answers your question directly, but the nuggets there should lead you to an answer.




回答3:


http://developer.android.com/reference/android/widget/Chronometer.html

For set the base time you can use elapsedRealtime(), and you can output format with setFormat()



来源:https://stackoverflow.com/questions/14788242/android-chronometer-setbase-in-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!