java - [eclipse]I want to add another move for my button in the timer -


this question exact duplicate of:

guys new eclipse, practicing timer in eclipse, can have codes? code below if clicked trigger button while counting, trigger counting without stop previous counting.

i have tried chancel button or reactive after down counting, still want know how make same trigger button acquire restart counting function. can u me directly embed code please? know new eclipse, java eng, everything, need explanation if not going take take much.

button countdownbutton2 = (button) findviewbyid(r.id.countdown1);       countdownbutton2.setonclicklistener(new view.onclicklistener() {     public void onclick(view view){            countdowntimer timer2 = new countdowntimer(3000,1000){                  @override                 public void onfinish() {                      mtimelabel1.settext("times baby!");                 }                  @override                 public void ontick(long millisuntilfinished) {                                                       int seconds = (int) (millisuntilfinished / 1000);                       int minutes = seconds / 60;                       seconds = seconds % 60;                        mtimelabel1.settext("" + minutes + ":"                                                       + string.format("%02d", seconds));                  }                     }.start();      } }); 

your current solution creating new countdowntimer not stopping old one. therefore if click button again have 2 countdown timers. when "new countdowntimer", when create it. therefore, before create new one, make sure stop previous one.

    button countdownbutton2 = (button) findviewbyid(r.id.countdown1);           countdownbutton2.setonclicklistener(new view.onclicklistener() {        countdowntimer timer2;     public void onclick(view view){            if(timer2!=null){               timer2.cancel();           }           timer2 = new countdowntimer(3000,1000){                  @override                 public void onfinish() {                      mtimelabel1.settext("times baby!");                 }                  @override                 public void ontick(long millisuntilfinished) {                                                       int seconds = (int) (millisuntilfinished / 1000);                       int minutes = seconds / 60;                       seconds = seconds % 60;                        mtimelabel1.settext("" + minutes + ":"                                                       + string.format("%02d", seconds));                  }                     }.start();      } }); 

explanation

previously, each time clicked button had new countdowntimer. each countdown timer changing value of label mtimelabel1. new timer created because inside on click lister, doing this:

countdowntimer2 timer2 = new countdowntimer(3000,1000) 

this means, create new timer, , keep reference called timer2. after onclick method finished, timer keeps running. however, when click again create new timer , new reference old timer lost. reference not old 1 new one.

so, instead make sure click listener has 1 timer. move reference outside of onclick method can keep reference timer. can make sure check timer cancel before starting one. however, if first time have clicked on button timer null, because "new countdowntimer" hasn't yet been called. so, check not null (it's not first button press), , if not cancel old 1 before creating new one. if first button press, don't want call cancel because there no timer cancel.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -