android使用CountDownTimer类,实现类似抢购的倒计时控件
来源:互联网 发布:lomo拍立得知乎 编辑:程序博客网 时间:2024/05/21 09:25
CountDownTimer由系统提供的。
倒计时核心代码:
参数:
MyCountDownTimer mc = new MyCountDownTimer(280000, 100)mc.start();
mc.start();方法开始
mc.cancel();方法结束
<span style="font-size:14px;"> /** * 继承 CountDownTimer 防范 * * 重写 父类的方法 onTick() 、 onFinish() */ class MyCountDownTimer extends CountDownTimer { /** * * @param millisInFuture * 表示以毫秒为单位 倒计时的总数 * * 例如 millisInFuture=1000 表示1秒 * * @param countDownInterval * 表示 间隔 多少微秒 调用一次 onTick 方法 * * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick() * */ public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() {//结束时调用 } /** * 处理时间倒计时进行页面刷新 * @param millisUntilFinished */ @Override public void onTick(long millisUntilFinished) { int ss = 1000; int mi = ss * 60; long minute = millisUntilFinished/ mi; long second = (millisUntilFinished- minute * mi) / ss; long milliSecond = millisUntilFinished - minute * mi - second * ss; String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟 String strSecond = second < 10 ? "0" + second : "" + second;//秒 String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒 strMilliSecond = milliSecond >100 ? strMilliSecond.substring(0,strMilliSecond.length()-1) : "" + strMilliSecond; tv_status.setText(strMinute + " 分 "+strSecond+"秒"+strMilliSecond); } }</span>
完整代码:
public class CommodityBuyView extends LinearLayout { private TextView tv_goods_desc,tv_status,tv_awardee; private MyCountDownTimer mc; private LinearLayout ll_result; private String awardee; public CommodityBuyView(Context context) { this(context, null); } public CommodityBuyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CommodityBuyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } /** * find 控件,初始化 * @param context */ private void initView(Context context) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.view_commodity_buying,this); tv_goods_desc= (TextView) view.findViewById(R.id.tv_goods_desc); tv_status= (TextView) view.findViewById(R.id.tv_status); tv_awardee= (TextView) view.findViewById(R.id.tv_awardee); ll_result= (LinearLayout) view.findViewById(R.id.ll_result); tv_status=(TextView) view.findViewById(R.id.tv_status); mc = new MyCountDownTimer(280000, 100); mc.start(); } /** * 继承 CountDownTimer 防范 * * 重写 父类的方法 onTick() 、 onFinish() */ class MyCountDownTimer extends CountDownTimer { /** * * @param millisInFuture * 表示以毫秒为单位 倒计时的总数 * * 例如 millisInFuture=1000 表示1秒 * * @param countDownInterval * 表示 间隔 多少微秒 调用一次 onTick 方法 * * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick() * */ public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { tv_status.setText("揭晓中...."); new Handler().postDelayed(new Runnable() { @Override public void run() { tv_status.setVisibility(View.GONE); ll_result.setVisibility(View.VISIBLE); tv_awardee.setText(awardee); } }, 5000); } /** * 处理时间倒计时进行页面刷新 * @param millisUntilFinished */ @Override public void onTick(long millisUntilFinished) { int ss = 1000; int mi = ss * 60; long minute = millisUntilFinished/ mi; long second = (millisUntilFinished- minute * mi) / ss; long milliSecond = millisUntilFinished - minute * mi - second * ss; String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟 String strSecond = second < 10 ? "0" + second : "" + second;//秒 String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒 strMilliSecond = milliSecond >100 ? strMilliSecond.substring(0,strMilliSecond.length()-1) : "" + strMilliSecond; tv_status.setText(strMinute + " 分 "+strSecond+"秒"+strMilliSecond); } } /** * 设置获奖人 * @param awardee */ public void setAwardee(String awardee){ this.awardee= awardee; }}
</pre><pre code_snippet_id="1861900" snippet_file_name="blog_20160901_5_44330" name="code" class="java">xml布局
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <ImageView android:id="@+id/iv_goods_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/goods" android:layout_marginTop="10dp" android:scaleType="fitXY"/> <TextView android:id="@+id/tv_goods_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginTop="20dp" android:text="iphone6s 128G (颜色随机)"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > <TextView android:id="@+id/tv_status" android:layout_width="match_parent" android:layout_height="40dp" android:textSize="16sp" android:textColor="#ffffff" android:background="@drawable/bg" android:layout_centerInParent="true" android:gravity="center" android:text="揭晓中..."/> <LinearLayout android:id="@+id/ll_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="10dp" android:paddingRight="10dp" android:gravity="center" android:visibility="gone" android:layout_centerInParent="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:padding="5dp" android:text="中奖" android:background="@drawable/et_bg"/> <TextView android:id="@+id/tv_awardee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginLeft="10dp" android:text="bb"/> </LinearLayout> </RelativeLayout></LinearLayout>
0 0
- android使用CountDownTimer类,实现类似抢购的倒计时控件
- Android 使用CountDownTimer实现倒计时的功能
- Android 使用CountDownTimer实现倒计时
- android的倒计时类CountDownTimer使用
- Android倒计时类CountDownTimer的简单使用
- Android倒计时CountDownTimer的使用
- Android倒计时CountDownTimer的使用
- android 倒计时CountDownTimer的使用
- Android倒计时 CountDownTimer的使用
- Android CountDownTimer 倒计时实现
- 使用CountDownTimer实现倒计时
- Android实现倒计时之使用CountDownTimer
- Android 使用CountDownTimer实现倒计时动画
- Android使用CountDownTimer实现倒计时功能
- Android实现倒计时之使用CountDownTimer
- android倒计时,使用CountDownTimer
- Android 倒计时CountDownTimer使用
- android listview列表实现倒计时,类似商品抢购列表
- Android 内存优化
- [BetterExplained]为什么你应该(从现在开始就)写博客
- 第2课 掌握C++
- eclipse-maven-sourceTree配置和简单使用
- Linux基本命令(三)
- android使用CountDownTimer类,实现类似抢购的倒计时控件
- MyBatis配置详解
- 第3课 MFC框架程序
- Apache Phoenix的Array类型
- Nginx源代码分析之群惊问题(十七)
- oracle.jbo.RowInconsistentException: JBO-25014: Another user has changed the row with primary key or
- 第4课 简单绘图
- C#之运算符重载学习案例
- 第5课 文本编程