Today's project used to buy the countdown function, today to find a lot of buddies, but also Baidu a lot, but did not like and can digest. Finally found a can easily read, but not meet the needs I want, so I do it. I don't know if it's easy, but at least the project's functionality can be achieved. (one afternoon, not in vain.) Happy)
directly on the code well, I believe all can understand. Except me. (I've been doing this all afternoon) Anyway, now I know something.
Package com.qust.widght;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import COM.QUST.RUSHBUYCOUNTDOWNTIMERVIEW.R; @SuppressLint ("Handlerleak") public class Rushbuycountdowntimerview extends LinearLayout {//day, 10-bit private TEXTVI
EW Tv_day_decade;
Days, a bit private TextView tv_day_unit;
Hours, 10-bit private TextView tv_hour_decade;
Hours, a bit private TextView tv_hour_unit;
Minutes, 10-bit private TextView tv_min_decade;
Minutes, a bit private TextView tv_min_unit;
Seconds, 10 private TextView tv_sec_decade; Seconds, a bit private TextView tv_sec_unit;
private context;
private int day_decade;
private int day_unit;
private int hour_decade;
private int hour_unit;
private int min_decade;
private int min_unit;
private int sec_decade;
private int sec_unit;
Timer private timer timer;
Private Handler Handler = new Handler () {public void Handlemessage (msg) {countdown ();
};
};
private int day = 0;
private int hour = 0;
private int min = 0;
private int sec = 0;
Public Rushbuycountdowntimerview (context, AttributeSet attrs) {Super (context, attrs);
This.context = context;
Layoutinflater Inflater = (layoutinflater) context. Getsystemservice (Context.layout_inflater_service);
View view = Inflater.inflate (R.layout.view_countdowntimer, this);
Tv_hour_decade = (TextView) View.findviewbyid (R.id.tv_hour_decade); Tv_hour_unit = (TextView) View.findviewbyid (R. Id.tv_hour_unit);
Tv_day_decade = (TextView) View.findviewbyid (R.id.tv_day_decade);
Tv_day_unit = (TextView) View.findviewbyid (r.id.tv_day_unit);
Tv_min_decade = (TextView) View.findviewbyid (R.id.tv_min_decade);
Tv_min_unit = (TextView) View.findviewbyid (r.id.tv_min_unit);
Tv_sec_decade = (TextView) View.findviewbyid (R.id.tv_sec_decade);
Tv_sec_unit = (TextView) View.findviewbyid (r.id.tv_sec_unit); /** * * @Description: Start timing * @param * @return void * @throws/public void star
T () {if (timer = null) {timer = new timer (); Timer.schedule (New TimerTask () {@Override public void run () {handle
R.sendemptymessage (0);
}, 0, 1000); }/** * * @Description: Stop timing * @param * @return void * @throws/public Voi D Stop () {if (Timer!= null) {timer.cancel ();
timer = null; }//if: sum = 12345678 public void addtime (int sum) {//Find the number of days int day = SUM/60/60/24
;
int day_time = sum% 24;
LOG.E ("Hour", Day + "");
LOG.E ("hour", sum% 24 + "");
Find out the hour//int hour = DAY_TIME/60;
int hour_time = day_time% 60;
LOG.E ("Hour", Hour + "");
First get a second value int sec = sum% 60; If more than 60 seconds, get minutes.
(number of seconds) int sec_time = SUM/60;
Then get the minute int min = sec_time% 60;
If it is greater than 60 minutes, get the hour (number of minutes).
int min_time = SEC_TIME/60;
Get the hour int hour = min_time% 24;
The remaining nature is the days of day = min_time/24;
LOG.E ("Min", min + "");
//Find out seconds//LOG.E ("seconds", sec + "");
SetTime (day, Hour, Min, sec); /** * @throws Exception * * @DescriptiOn: Set the countdown length * @param * @return void * @throws/public void settime (int ' Day, int hour, int min,
int sec) {//The number of days here does not write also line, I write 365 if (Day >= 365 | | hour >= | | min >= | | sec >= | |-day < 0 || Hour < 0 | | Min < 0 | | SEC < 0) {throw new RuntimeException ("Time format Yes error,please check out your code
");
//day's 10-digit day_decade = DAY/10;
Day's single-digit number, here the remainder on the line day_unit = Day-day_decade * 10;
Hour_decade = HOUR/10;
Hour_unit = Hour-hour_decade * 10;
Min_decade = MIN/10;
Min_unit = Min-min_decade * 10;
Sec_decade = SEC/10;
Sec_unit = Sec-sec_decade * 10;
The first time to initialize the Timeclean ();
private void Timeclean () {Tv_day_decade.settext (Day_decade + "");
Tv_day_unit.settext (Day_unit + "");
Tv_hour_decade.settext (Hour_decade + ""); Tv_hour_unit.settext (Hour_unit + "");
Tv_min_decade.settext (Min_decade + "");
Tv_min_unit.settext (Min_unit + "");
Tv_sec_decade.settext (Sec_decade + "");
Tv_sec_unit.settext (Sec_unit + ""); /** * * @Description: Countdown * @param * @return Boolean * @throws/public Boolean
Countdown () {if (Iscarry4unit (Tv_sec_unit)) {if (Iscarry4decade (Tv_sec_decade)) { if (Iscarry4unit (Tv_min_unit)) {if (Iscarry4decade (Tv_min_decade)) {if (IS)
Day4unit (Tv_hour_unit)) {if (Isday4decade (Tv_hour_decade)) {
if (Isday4unit (Tv_day_unit)) {if (Isday4decade (Tv_day_decade)) { Toast.maketext (Context, "Time is Up", toast.length_short). s
how (); Tv_day_decade.settext ("0");
Tv_day_unit.settext ("0");
Tv_hour_decade.settext ("0");
Tv_hour_unit.settext ("0");
Tv_min_decade.settext ("0");
Tv_min_unit.settext ("0");
Tv_sec_decade.settext ("0");
Tv_sec_unit.settext ("0");
Stop ();
return false;
}
}
}
}
}
}} return false; /** * Conduct-minutes, determine single-digit * * @Description: Change 10 bits, and determine whether the need to carry * @param * @return Boolean * @t Hrows * * Private Boolean Iscarry4decaDe (TextView TV) {int time = integer.valueof (Tv.gettext (). toString ());
Time = time-1;
if (Time < 0) {time = 5;
Tv.settext (Time + "");
return true;
else {Tv.settext (time + "");
return false;
}/** *-time and minutes, Judge Single-digit * * @Description: Change a bit, and determine whether the need to carry * @param * @return Boolean * @throws/Private Boolean iscarry4unit (TextView TV) {int time = integer.valueof (Tv.gettext (). toSt
Ring ());
Time = time-1;
if (Time < 0) {time = 9;
Tv.settext (Time + "");
return true;
else {Tv.settext (time + "");
return false;
}/** *-minutes, determine single-digit * * @Description: Change 10 digits, and determine whether the need to carry * @param * @return Boolean * @throws/Private Boolean isday4unit (TextView TV) {int time = INTEGER.VALUEOF (TV. GetText (). toString ());
Time = time-1;
if (Time < 0) {time = 3;
Tv.settext (Time + "");
return true;
else {Tv.settext (time + "");
return false;
}/** *-time and minutes, Judge Single-digit * * @Description: Change a bit, and determine whether the need to carry * @param * @return Boolean * @throws/Private Boolean Isday4decade (TextView TV) {int time = integer.valueof (Tv.gettext (). toSt
Ring ());
Time = time-1;
if (Time < 0) {time = 2;
Tv.settext (Time + "");
return true;
else {Tv.settext (time + "");
return false;
}
}
}
Called by the main class
Package com.qust.widght;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.Toast;
Import COM.QUST.RUSHBUYCOUNTDOWNTIMERVIEW.R;
public class Mainactivity extends activity {
private rushbuycountdowntimerview timerview;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Timerview = (Rushbuycountdowntimerview) Findviewbyid (R.id.timerview);
Set time (DAY,HOUR,MIN,SEC)
//timerview.settime (0, 0, 0, 5);
int sum =;
Pass the number of seconds to the countdown method.
timerview.addtime (sum);
Start Countdown
Timerview.start ();
}
Layout file
<linearlayout 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:or" ientation= "vertical" > <com.qust.widght.rushbuycountdowntimerview android:id= "@+id/timerview" a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" > </com.qust.widght.rushbuycount Downtimerview> </linearlayout>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:background=" @android : Color/white "android:orientation=" horizontal "android:padding=" 10DP "> <textview android:id=" @ +id/tv_day_decade "style=" @style/rushbuycountdowntimerviewstyle "/> <textview android:id=" @+id/ Tv_day_unit "style=" @style/rushbuycountdowntimerviewstyle "android:layout_marginleft=" 1DP "/> < TextView android:layout_width= "wrap_content" android:layout_height= "Match_parent" Android:backgrou
Nd= "@android: Color/white" android:gravity= "center" android:text= ":" Android:textcolor= "#4F4242" Android:textsize= "30sp"/> <textview android:id= "@+id/tv_hour_decade" style= "@style/RUSHB
Uycountdowntimerviewstyle "/> <textview android:id= "@+id/tv_hour_unit" style= "@style/rushbuycountdowntimerviewstyle" Andr oid:layout_marginleft= "1DP"/> <textview android:layout_width= "Wrap_content" Android:layout_hei ght= "Match_parent" android:background= "@android: Color/white" android:gravity= "center" Android:text = ":" android:textcolor= "#4F4242" android:textsize= "30sp"/> <textview android:id= "@+id/ Tv_min_decade "style=" @style/rushbuycountdowntimerviewstyle "/> <textview android:id=" @+id/tv_m In_unit "style=" @style/rushbuycountdowntimerviewstyle "android:layout_marginleft=" 1DP "/> <Text View android:layout_width= "wrap_content" android:layout_height= "Match_parent" android:background= "
@android: Color/white "android:gravity=" center "android:text=": "Android:textcolor=" #4F4242 " Android:textsize= "30sp"/> <textview android:id=" @+id/tv_sec_decade style= "@style/rushbuycountdowntimerviewstyle"/&
Gt <textview android:id= "@+id/tv_sec_unit" style= "@style/rushbuycountdowntimerviewstyle" android:l ayout_marginleft= "1DP"/> </LinearLayout>
The
wants to help those in need. You can also take a look at the
when you forget.