android - Start timer on a button click and stop it with 10 button clicks -
so far have button tap counter working , stops @ 10 taps. problem timer. every time press button resets timer. want button start timer on first click , stop timer on 10th click, stumped , not know how code this. know how can working?
java code:
package com.example.thirtytapgametest; import android.os.bundle; import android.os.handler; import android.os.systemclock; import android.view.view; import android.widget.imagebutton; import android.widget.textview; import android.app.activity; public class mainactivity extends activity { private int mcount = 0; private imagebutton startbutton; private textview timervalue; private long starttime = 0l; private handler customhandler = new handler(); long timeinmilliseconds = 0l; long timeswapbuff = 0l; long updatedtime = 0l; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final textview counttextview = (textview) findviewbyid(r.id.textviewcount); timervalue = (textview) findviewbyid(r.id.timervalue); startbutton = (imagebutton) findviewbyid(r.id.imagebutton1); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { starttime = systemclock.uptimemillis(); customhandler.postdelayed(updatetimerthread, 0); mcount++; counttextview.settext("taps: " + mcount); if(mcount == 10) {view.setenabled(false);} } }); } private runnable updatetimerthread = new runnable() { public void run() { timeinmilliseconds = systemclock.uptimemillis() - starttime; updatedtime = timeswapbuff + timeinmilliseconds; int secs = (int) (updatedtime / 1000); int mins = secs / 60; secs = secs % 60; int milliseconds = (int) (updatedtime % 1000); timervalue.settext("" + mins + ":" +string.format("%02d", secs) + ":" +string.format("%03d", milliseconds)); customhandler.postdelayed(this, 0); } }; }
xml code:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:background="@drawable/thirty_tap_game_background"> <imagebutton android:id="@+id/imagebutton1" android:layout_width="200dp" android:layout_height="100dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="138dp" android:background="@drawable/test_play_button" /> <textview android:id="@+id/timervalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imagebutton1" android:layout_centerhorizontal="true" android:layout_marginbottom="32dp" android:textsize="50sp" android:textcolor="#ff0000" android:text="@string/timerval" /> <textview android:id="@+id/textviewcount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="73dp" android:text="@string/countval" android:textsize="30sp" android:textcolor="#ff0000"/> </relativelayout>
below code resolve problem. 'timeswapbuff. unused, i've commented line of code.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final textview counttextview = (textview) findviewbyid(r.id.textviewcount); timervalue = (textview) findviewbyid(r.id.timervalue); startbutton = (imagebutton) findviewbyid(r.id.imagebutton1); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { if (mcount ==0) { starttime = systemclock.uptimemillis(); customhandler.postdelayed(updatetimerthread, 0); } mcount++; counttextview.settext("taps: " + mcount); if(mcount == 10) { view.setenabled(false); customhandler.removecallbacks(updatetimerthread); } } }); } private runnable updatetimerthread = new runnable() { public void run() { updatedtime = systemclock.uptimemillis() - starttime; // updatedtime = timeswapbuff + timeinmilliseconds; int secs = (int) (updatedtime / 1000); int mins = secs / 60; secs = secs % 60; int milliseconds = (int) (updatedtime % 1000); timervalue.settext("" + mins + ":" +string.format("%02d", secs) + ":" +string.format("%03d", milliseconds)); customhandler.postdelayed(this, 0); } }; }
Comments
Post a Comment