This article undertakes, Android Development V-bomb: simple clock (alarm clock) and Android development six bullets: Simple clock (timer), this part is about the stopwatch.
Layout
Also create a new Class (Stopwatchview) and extend it from linearlayout, and use it as a layout.
<myapplication.nomasp.com.clock.stopwatchview android:ID="@+id/tabstopwatch"Android:layout_width ="Match_parent"Android:layout_height ="Match_parent"Android:orientation ="Vertical"> <linearlayout android:layout_width ="Match_parent"Android:layout_height ="Wrap_content"Android:orientation ="Horizontal"> <textview android:ID="@+id/tvhour"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:text=":"Android:layout_width ="Wrap_content"Android:layout_height ="Wrap_content"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:ID="@+id/tvminute"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:text=":"Android:layout_width ="Wrap_content"Android:layout_height ="Wrap_content"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:ID="@+id/tvsecond"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:text="."Android:layout_width ="Wrap_content"Android:layout_height ="Wrap_content"Android:textappearance ="? Android:attr/textappearancelarge"/> <textview android:ID="@+id/tvmsceond"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:textappearance ="? Android:attr/textappearancelarge"/> </linearlayout> <listview Android:ID="@+id/lvwatchtimelist"Android:layout_width ="Match_parent"Android:layout_height ="0DP"Android:layout_weight ="1"> </listview> <linearlayout android:orientation ="Horizontal"Android:layout_width ="Match_parent"Android:layout_height ="Wrap_content"> <button android:ID="@+id/btnswstart"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:text="@string/start"/> <button android:ID="@+id/btnswpause"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:text="@string/pause"/> <button android:ID="@+id/btnswresume"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:text="@string/resume"/> <button android:ID="@+id/btnswrecord"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:text="@string/record"/> <button android:ID="@+id/btnswreset"Android:layout_width ="0DP"Android:layout_height ="Wrap_content"Android:layout_weight ="1"Android:text="@string/reset"/> </linearlayout></myapplication.nomasp.com.clock.stopwatchview>
Stopwatchview
It is also the first thing to define these balabala:
Private intTenmsecs =0;PrivateTimer timer =NewTimer ();PrivateTimerTask TimerTask =NULL;PrivateTimerTask Showtimetask =NULL;PrivateTextView Tvhour, Tvminute, Tvsecond, Tvmsecond;PrivateButton Btnswstart, Btnswresume, Btnswreset, Btnswpause, Btnswrecord;PrivateListView lvwatchtimelist;PrivateArrayadapter<string> adapter;Private Static Final intMsg_what_show_time =1; Public Stopwatchview(Context context) {Super(context); } Public Stopwatchview(context context, AttributeSet attrs) {Super(context, attrs); } Public Stopwatchview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }
Look at these comments, and found that there is no difference between the previous two articles, Ah, I will not talk directly on the code good.
@Override protected void onfinishinflate(){Super. Onfinishinflate ();//For each corresponding TextView control set to 0Tvhour = (TextView) Findviewbyid (R.id.tvhour); Tvhour.settext ("0"); Tvminute = (TextView) Findviewbyid (R.id.tvminute); Tvminute.settext ("0"); Tvsecond = (TextView) Findviewbyid (R.id.tvsecond); Tvsecond.settext ("0"); Tvmsecond = (TextView) Findviewbyid (R.id.tvmsceond); Tvmsecond.settext ("0");//Set listener events for each buttonBtnswrecord = (Button) Findviewbyid (R.id.btnswrecord); Btnswrecord.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {Adapter.insert (String.Format ("%d:%d:%d.%d", tenmsecs/ -/ -/ -, tenmsecs/ -/ -% -, tenmsecs/ -% -, tenmsecs% -),0); } }); Btnswpause = (Button) Findviewbyid (r.id.btnswpause); Btnswpause.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {//StopStoptimer (); Btnswpause.setvisibility (View.gone); Btnswresume.setvisibility (view.visible); Btnswreset.setvisibility (view.visible); Btnswrecord.setvisibility (View.gone); } }); Btnswreset = (Button) Findviewbyid (R.id.btnswreset); Btnswreset.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {//StopStoptimer (); Tenmsecs =0; Adapter.clear (); Btnswstart.setvisibility (view.visible); Btnswpause.setvisibility (View.gone); Btnswreset.setvisibility (View.gone); Btnswrecord.setvisibility (View.gone); Btnswresume.setvisibility (View.gone); } }); Btnswresume = (Button) Findviewbyid (R.id.btnswresume); Btnswresume.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {//StartStarttimer (); Btnswresume.setvisibility (View.gone); Btnswreset.setvisibility (View.gone); Btnswrecord.setvisibility (view.visible); Btnswpause.setvisibility (view.visible); } }); Btnswstart = (Button) Findviewbyid (R.id.btnswstart); Btnswstart.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {//StartStarttimer (); Btnswstart.setvisibility (View.gone); Btnswpause.setvisibility (view.visible); Btnswrecord.setvisibility (view.visible); } }); Btnswrecord.setvisibility (View.gone); Btnswpause.setvisibility (View.gone); Btnswreset.setvisibility (View.gone); Btnswresume.setvisibility (View.gone);//Add adapter to listLvwatchtimelist = (ListView) Findviewbyid (r.id.lvwatchtimelist); adapter =NewArrayadapter<string> (GetContext (), Android. R.layout.simple_list_item_1); Lvwatchtimelist.setadapter (adapter);//Send a message to handlerShowtimetask =NewTimerTask () {@Override Public void Run() {handler.sendemptymessage (msg_what_show_time); } };//Start timingTimer.schedule (Showtimetask, $, $); }//Start Private void Starttimer(){if(TimerTask = =NULL) {TimerTask =NewTimerTask () {@Override Public void Run() {tenmsecs++; } }; Timer.schedule (TimerTask,Ten,Ten); } }//End Private void Stoptimer(){if(TimerTask! =NULL) {timertask.cancel (); TimerTask =NULL; } }//Cancel Timings Public void ondestory() {timer.cancel (); }PrivateHandler Handler =NewHandler () { Public void Handlemessage(Message msg) {Switch(Msg.what) {//If the message matches, the corresponding time is calculated and displayed on the corresponding TextView CaseMSG_WHAT_SHOW_TIME:tvHour.setText (tenmsecs/ -/ -/ -+""); Tvminute.settext (tenmsecs/ -/ -% -+""); Tvsecond.settext (tenmsecs/ -% -+""); Tvmsecond.settext (tenmsecs% -+""); Break;default: Break; } }; };
End
Well, this time it's really over.
Similarly, you need the code to comment on the email directly. The code will continue to update, and the comments will continue to update ...
Project also uploaded to GitHub, welcome to contribute code AH-Portal.
Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp
Android Development Seventh Bullet: Simple Clock (stopwatch)