Custom Control----Countdown control

Source: Internet
Author: User
Tags time and seconds vmin

First on:

Because the time and seconds have their own background color layout, so rewrite a textview is not flexible enough, so we customize a timetextview inherit from LinearLayout and then put a few textview in it.
Let's look at the layout file first:

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:layout_marginbottom="@dimen/default_margin"android:layout_marginleft="@dimen/default_margin"android:layout_marginright="@dimen/default_margin"Android:background="@android: color/transparent"android:gravity="Center"android:orientation="Horizontal"android:padding="@dimen/default_margin_small"> <textview android:id="@+id/tv_hours"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:background="@drawable/background_btn_red"android:padding="@dimen/default_margin_small"android:text=" the"Android:textcolor="@color/white"Android:textsize="@dimen/text_default"/> <textview android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text=":"Android:textcolor="@color/default_text_red"Android:textsize="@dimen/text_default"/> <textview android:id="@+id/tv_minutes"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:background="@drawable/background_btn_red"android:padding="@dimen/default_margin_small"android:text="XX"Android:textcolor="@color/white"Android:textsize="@dimen/text_default"/> <textview android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text=":"Android:textcolor="@color/default_text_red"Android:textsize="@dimen/text_default"/> <textview android:id="@+id/tv_seconds"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:background="@drawable/background_btn_red"android:padding="@dimen/default_margin_small"android:text="XX"Android:textcolor="@color/white"Android:textsize="@dimen/text_default"/></linearlayout>

And then it's the custom linearlayout,

/** * Countdown 3 Text * Created by Fangchao on 2015/3/12 */ Public  class timetextview extends linearlayout {    Private LongMday, Mhour, Mmin, Msecond;//day, hour, minute, second    Private BooleanRun =false;//Did you start theTimer timer =NewTimer (); TextView Vhour, Vmin, vseconds; Public Timetextview(Context context) {Super(context);    INIUI (context); } Public Timetextview(context context, AttributeSet attrs) {Super(context, attrs);    INIUI (context); } Public void INIUI(Context context)        {Layoutinflater Minflater = layoutinflater.from (context); View MyView = minflater.inflate (R.layout.view_time_texviews,NULL);        Vhour = (TextView) Myview.findviewbyid (r.id.tv_hours);        Vmin = (TextView) Myview.findviewbyid (r.id.tv_minutes);        Vseconds = (TextView) Myview.findviewbyid (r.id.tv_seconds);    AddView (MyView); } Public Timetextview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    INIUI (context); }PrivateHandler Mhandler =NewHandler () {}; Public Boolean Isrun() {returnRun } Public void Setrun(BooleanRun) { This. Run = run; } Public void Start() {if(!isrun ()) {Setrun (true); Timer.schedule (Task, +, +); }    }/** * Assign values to TextView according to the time difference passed in * * @param Duration */     Public void Settimes(LongDuration) {Date Date =NewDate (duration); Date date1 =NewDate (1L);/*mday = duration/60000/60/24;        Mhour = (Duration-mday * 6000 * 60 * 24)/3600000;        Mmin = (Duration-mhour * 6000 * 60-mday * 3600000 * 24)/60000; Msecond = (duration-mmin * 60000-mhour * 3600000-mday * 3600000 *)/60000;*///need to be modified, test withMday = Date.getday ();        Mhour = Date.gethours ();        Mmin = Date.getminutes ();    Msecond = Date.getseconds (); }/** * Countdown calculation * /    Private void Computetime() {msecond--;if(Msecond <0) {mmin--; Msecond = -;if(Mmin <0) {mmin = -; mhour--;if(Mhour <0) {//Countdown EndMhour = -;                mday--; }}}} TimerTask task =NewTimerTask () {@Override         Public void Run() {Mhandler.post (NewRunnable () {//UI thread                @Override                 Public void Run() {run =true; Computetime ();if(Mday <0) {setvisibility (view.gone); Setrun (false); } vhour.settext (Mhour <Ten? ("0"+ Mhour): Mhour +""); Vseconds.settext (Msecond <Ten? ("0"+ Msecond): Msecond +""); Vmin.settext (Mmin <Ten? ("0"+ mmin): Mmin +"");        }            }); }    };}

Just put the timetextview directly in the place where you need it,
For example, you can set a countdown time in adapter ....

@Override public void Onbindheaderview (Recyclerview. ViewholderHolder, int position) {Headerviewholder Headviewholder = (headerviewholder) holder;Long duration=1426244976513L + $* +-Timeutils. Getcurrenttimeinlong();Log. E("long///////", Timeutils. Getcurrenttimeinlong() +"");if (!headviewholder. Timetextview. Isrun()) {Headviewholder. Timetextview. Settimes(duration);Headviewholder. Timetextview. Start();}    }

Custom Control----Countdown 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.