你好,游客 登录 注册 搜索
背景:
阅读新闻

Android控件倒计时的实现

[日期:2013-06-08] 来源:Linux社区  作者:wuzoujing [字体:大 中 小]

Android控件倒计时的实现,使用CountDownTimer实现比较简单,以下是将Button对象传进来的TimeCountButton类。

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

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android控件  Android倒计时 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款