Timerview.java
PackageCom.mytest.myclock;ImportJava.util.Timer;ImportJava.util.TimerTask;ImportAndroid.app.AlertDialog;ImportAndroid.content.Context;ImportAndroid.os.Handler;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classTimerviewextendsLinearLayout { PublicTimerview (Context context) {Super(context); //TODO auto-generated Constructor stub } PublicTimerview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub } PublicTimerview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub } PrivateTextView Tvhour; PrivateTextView Tvminute; PrivateTextView Tvsecond; PrivateButton btnstart; PrivateButton Btnstop; @Overrideprotected voidonfinishinflate () {//TODO auto-generated Method Stub Super. Onfinishinflate (); Init (); } Private voidinit () {Tvhour= (TextView) This. Findviewbyid (R.id.timer_hour); Tvminute= (TextView) This. Findviewbyid (R.id.timer_mini); Tvsecond= (TextView) This. Findviewbyid (R.id.timer_second); Btnstart= (Button) This. Findviewbyid (R.id.btn_start_timer); Btnstop= (Button) This. Findviewbyid (R.id.btn_stop_timer); Btnstop.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {stoptimer (); } }); Btnstart.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {starttimer (); } }); } Private Final intwork_state_stop = 0;//timekeeping status, stopping Private Final intWork_state_run = 1;//Timing Status Private intUserinputtime = 0; PrivateTimer timer; Privatetimertask task; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseWork_state_run:inthour = USERINPUTTIME/60/60; intminute = (USERINPUTTIME/60)%60; intSecond = userinputtime%60; Tvhour.settext (""+hour); Tvminute.settext (""+minute); Tvsecond.settext (""+second); Break; CaseWork_state_stop:NewAlertdialog.builder (GetContext ()). Settitle ("Time to"). Setnegativebutton ("Cancel",NULL). Show (); Stoptimer (); Break; } }; }; Private voidStarttimer () {Try{userinputtime= (Integer.parseint (Tvhour.gettext (). toString ()) * * + integer.parseint (Tvminute.gettext (). Tostri NG ()) * 60 +Integer.parseint (Tvsecond.gettext (). toString ())); } Catch(Exception e) {LOG.E ("Info", "Timerview->starttimer" +e.getmessage ()); return; } Timer=NewTimer (); Task=NewTimerTask () {@Override Public voidrun () {Userinputtime--; Handler.sendemptymessage (Work_state_run); if(Userinputtime <=0) {handler.sendemptymessage (work_state_stop); Stoptimer (); } } }; Timer.schedule (Task,1000,1000);//delay one second and execute once every second Timertask.run () } Private voidStoptimer () {if(task!=NULL) {task.cancel (); Task=NULL; } }}
View Code
Summary:
1, using timer and timertask to refresh time;
2, the acquisition of hours should be hour = USERINPUTTIME/60/60, and not directly hour = userinputtime/360.
Android Alarm Clock (iv): Implement Timer