Countdown Time Control

Source: Internet
Author: User

In doing some e-commerce or shopping app, we often see the countdown to buy the logo, today just have time to use the project to extract the control, there is a need to refer to the following

======================================================================================================

All rights reserved, if need to reprint please indicate source: http://blog.csdn.net/you4580

=======================================================================================================

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Horizontal" >


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/ele_surplus"
Android:textcolor= "#999999"
Android:textsize= "14sp"/>


<textview
Android:id= "@+id/electricity_time_day"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "3DP"
android:layout_marginright= "3DP"
android:background= "@drawable/electricity_countdown"
android:gravity= "Center"
Android:minwidth= "24DP"
Android:padding= "3DP"
Android:textcolor= "#1A7FCF"
Android:textsize= "12SP"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Day"
Android:textcolor= "#999999"
Android:textsize= "14sp"/>


<textview
Android:id= "@+id/electricity_time_hour"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "3DP"
android:layout_marginright= "3DP"
android:background= "@drawable/electricity_countdown"
android:gravity= "Center"
Android:minwidth= "24DP"
Android:padding= "3DP"
Android:textcolor= "#1A7FCF"
Android:textsize= "12SP"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "When"
Android:textcolor= "#999999"
Android:textsize= "14sp"/>


<textview
Android:id= "@+id/electricity_time_min"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "3DP"
android:layout_marginright= "3DP"
android:background= "@drawable/electricity_countdown"
android:gravity= "Center"
Android:minwidth= "24DP"
Android:padding= "3DP"
Android:textcolor= "#1A7FCF"
Android:textsize= "12SP"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Min"
Android:textcolor= "#999999"
Android:textsize= "14sp"/>


<textview
Android:id= "@+id/electricity_time_mse"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "3DP"
android:layout_marginright= "3DP"
android:background= "@drawable/electricity_countdown"
android:gravity= "Center"
Android:minwidth= "24DP"
Android:padding= "3DP"
Android:textcolor= "#1A7FCF"
Android:textsize= "12SP"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Seconds"
Android:textcolor= "#999999"
Android:textsize= "14sp"/>


</LinearLayout>


======================================================================================================


/**
* Timetextview Countdown control
*
* @DESCRIPTION:
* @AUTHOR: you4580
* @VERSION: v1.0
* @DATE: 2015-4-20
*/
public class Timetextview extends linearlayout{
Private Context Mcontext;
Private TextView Mday,mhour,mmin,mmse;
Private long days,hours,minutes,seconds;
Private Boolean run = true; Is it started?
Private Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case 0:
if (run) {
if (Computetime ()) {
Setui ();
Handler.sendemptymessagedelayed (0, 1000);
}
}
Break
}
}
};
Public Timetextview (Context context) {
Super (context);
This.mcontext = context;
Inflatelayout ();
};


Public Timetextview (context context, AttributeSet Attrs) {
Super (context, attrs);
This.mcontext = context;
Inflatelayout ();
}


@SuppressLint ("Newapi")
Public Timetextview (context context, AttributeSet Attrs,int Defstyle) {
Super (context, attrs, Defstyle);
This.mcontext = context;
Inflatelayout ();
}


private void Inflatelayout () {
This.addview (Layoutinflater.from (Mcontext). Inflate (R.layout.time_textview, null));
Mday = (TextView) This.findviewbyid (r.id.electricity_time_day);
Mhour = (TextView) This.findviewbyid (R.id.electricity_time_hour);
Mmin = (TextView) This.findviewbyid (r.id.electricity_time_min);
MMse = (TextView) This.findviewbyid (R.ID.ELECTRICITY_TIME_MSE);
}


public void SetTime (String day,string hour,string minute,string second) {
if (Textutils.isempty (day)) {
days = 0;
}else{
Days = Long.parselong (day);
}


if (Textutils.isempty (hour)) {
Hours = 0;
}else{
Hours = Long.parselong (hour);
}


if (Textutils.isempty (minute)) {
minutes = 0;
}else{
minutes = Long.parselong (minute);
}


if (Textutils.isempty (second)) {
seconds = 0;
}else{
seconds = Long.parselong (second);
}
Setui ();

Handler.removemessages (0);

Send handler messages every 1 seconds

Handler.sendemptymessagedelayed (0,1000);
}


private void Setui () {
Mday.settext (String.valueof (days));
Mhour.settext (string.valueof (hours));
Mmin.settext (string.valueof (minutes));
Mmse.settext (string.valueof (seconds));
}


Private Boolean computetime () {
Boolean flag = true;
seconds--;
if (seconds < 0) {
minutes--;
seconds = 59;
if (minutes < 0) {
minutes = 59;
hours--;
if (Hours < 0) {
Hours = 23;
days--;
if (days<0) {
Flag = false;
}
}
}
}
return flag;
}


public void Stopcomputetime () {
Run = false;
}

}

==================================================================================================

To invoke the layout file of a custom control

<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: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= "com.example.timetextview.MainActivity";


   
    
     <com.example.timetextview.view.timetextview
        android:id= "@+id/ Electricity_countdown "
        android:layout_width=" wrap_content "
        android:layout_height= "Wrap_content"/>


</linearlayout>

===================================================================================================

public class Mainactivity extends Actionbaractivity {
Private Timetextview Mtimetextview;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

mtimetextview= (Timetextview) Findviewbyid (R.id.electricity_countdown);

Set an initial time for the control, usually the server gives back a set of time
Mtimetextview.settime ("5", "00", "00", "00");
}
}

=============================================================================================================== ========


Countdown Time Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.