java - Android Studio give a FOR loop a delay -


im trying build easy app user need remmber on buttons computer pressed .. each button clicked make sound .. in order make each sound ,sound correctly tried give little bit of delay between each click computer pressed...

so make int[] include sequence of way computer click buttons ( 1 click on button1 , 2 click on button 2 .... ) , im trying acually pressed button haha made :

   private void clickbuttons(int[] sequence) {         button btcomp1 = (button) findviewbyid(r.id.btcomp1);         button btcomp2 = (button) findviewbyid(r.id.btcomp2);         button btcomp3 = (button) findviewbyid(r.id.btcomp3);         button btcomp4 = (button) findviewbyid(r.id.btcomp4);         textview check=(textview)findviewbyid(r.id.textviewkq);         handler handler = new handler();          (int = 0; < sequence.length; i++)         {             switch (sequence[i]) {                 case 1:                     btcomp1.performclick();                     check.settext("4..");                     timedelay();                     break;                 case 2:                     btcomp2.performclick();                     check.settext("3..");                     timedelay();                      break;                 case 3:                     btcomp3.performclick();                     check.settext("2..");                     timedelay();                     break;                 case 4:                     btcomp4.performclick();                     check.settext("1..");                     timedelay();                     break;             }          }     } 

it didnt work , thought may problem button set changing textview seems enter 1 case ... , delay when mover firstactivity gameactivity

i have nothing in firstactivity field username ...

well hopes helped me guys , soory grammer ><

this delay .. tried delay me few seconds after gor actity 1 nothing in main ..

 private void timedelay() {     try{         thread.sleep(2500);     }     catch (exception e){      } } 

for kind of thing should use android.os.handler make timer. , in callback method, click buttons. method using block main thread. bad.

luckily, have written timer class this! here whole class:

import android.os.handler;  public class timer {     private handler handler;     private boolean paused;      private int interval;      private runnable task = new runnable () {         @override         public void run() {             if (!paused) {                 runnable.run ();                 timer.this.handler.postdelayed (this, interval);             }         }     };      private runnable runnable;      public int getinterval() {         return interval;     }      public void setinterval(int interval) {         this.interval = interval;     }      public void starttimer () {         paused = false;         handler.postdelayed (task, interval);     }      public void stoptimer () {         paused = true;     }      public timer (runnable runnable, int interval, boolean started) {         handler = new handler ();         this.runnable = runnable;         this.interval = interval;         if (started)             starttimer ();     } } 

try understand it. after that, can use timer this:

final timer t = new timer(new runnable() {     private int = 1;     button btcomp1 = (button) mainactivity.this.findviewbyid(r.id.btcomp1);     button btcomp2 = (button) mainactivity.this.findviewbyid(r.id.btcomp2);     button btcomp3 = (button) mainactivity.this.findviewbyid(r.id.btcomp3);     button btcomp4 = (button) mainactivity.this.findviewbyid(r.id.btcomp4);     textview check=(textview)mainactivity.this.findviewbyid(r.id.textviewkq);     public void run() {         switch (i) {             case 1:                 btcomp1.performclick();                 check.settext("4..");                 break;             case 2:                 btcomp2.performclick();                 check.settext("3..");                 break;             case 3:                 btcomp3.performclick();                 check.settext("2..");                 break;             case 4:                 btcomp4.performclick();                 check.settext("1..");                 t.stoptimer();                 break;         }     } }, 1000, true); 

once timer created, presses button once second. , set text of check. when presses fourth time, timer stops.

easy! (if have timer class)


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -