Android control countdown implementation

Source: Internet
Author: User

The CountDownTimer implementation is relatively simple. The following is the TimeCountButton class that transmits the Button object.

 

 

[Java]
Public class CountDownButton extends CountDownTimer {
Public static final int TIME_COUNT_FUTURE = 60000;
Public static final int TIME_COUNT_INTERVAL = 1000;

Private Context mContext;
Private Button mButton;
Private String mOriginalText;
Private Drawable mOriginalBackground;
Private Drawable mTickBackground;
Private int mOriginalTextColor;

Public CountDownButton (){
Super (TIME_COUNT_FUTURE, TIME_COUNT_INTERVAL );
}

Public CountDownButton (long millisInFuture, long countDownInterval ){
Super (millisInFuture, countDownInterval );
}

Public void init (Context context, Button button ){
This. mContext = context;
This. mButton = button;
This. mOriginalText = mButton. getText (). toString ();
This. mOriginalBackground = mButton. getBackground ();
This. mTickBackground = this. mOriginalBackground;
This. mOriginalTextColor = mButton. getCurrentTextColor ();
}

Public void setTickDrawable (Drawable tickDrawable ){
This. mTickBackground = tickDrawable;
}
 
@ Override
Public void onFinish (){
If (mContext! = Null & mButton! = Null ){
MButton. setText (mOriginalText );
MButton. setTextColor (mOriginalTextColor );
MButton. setBackgroundDrawable (mOriginalBackground );
MButton. setClickable (true );
}
}
 
@ Override
Public void onTick (long millisUntilFinished ){
If (mContext! = Null & mButton! = Null ){
MButton. setClickable (false );
MButton. setBackgroundDrawable (mTickBackground );
MButton. setTextColor (mContext. getResources (). getColor (android. R. color. darker_gray ));
MButton. setText (mOriginalText + "(" + millisUntilFinished/1000 + "')");
}
}
}

Public class CountDownButton extends CountDownTimer {
Public static final int TIME_COUNT_FUTURE = 60000;
Public static final int TIME_COUNT_INTERVAL = 1000;
 
Private Context mContext;
Private Button mButton;
Private String mOriginalText;
Private Drawable mOriginalBackground;
Private Drawable mTickBackground;
Private int mOriginalTextColor;
 
Public CountDownButton (){
Super (TIME_COUNT_FUTURE, TIME_COUNT_INTERVAL );
}
 
Public CountDownButton (long millisInFuture, long countDownInterval ){
Super (millisInFuture, countDownInterval );
}
 
Public void init (Context context, Button button ){
This. mContext = context;
This. mButton = button;
This. mOriginalText = mButton. getText (). toString ();
This. mOriginalBackground = mButton. getBackground ();
This. mTickBackground = this. mOriginalBackground;
This. mOriginalTextColor = mButton. getCurrentTextColor ();
}
 
Public void setTickDrawable (Drawable tickDrawable ){
This. mTickBackground = tickDrawable;
}

@ Override
Public void onFinish (){
If (mContext! = Null & mButton! = Null ){
MButton. setText (mOriginalText );
MButton. setTextColor (mOriginalTextColor );
MButton. setBackgroundDrawable (mOriginalBackground );
MButton. setClickable (true );
}
}

@ Override
Public void onTick (long millisUntilFinished ){
If (mContext! = Null & mButton! = Null ){
MButton. setClickable (false );
MButton. setBackgroundDrawable (mTickBackground );
MButton. setTextColor (mContext. getResources (). getColor (android. R. color. darker_gray ));
MButton. setText (mOriginalText + "(" + millisUntilFinished/1000 + "')");
}
}
}

 

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.