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 + "')");
}
}
}