android使用CountDownTimer类,实现类似抢购的倒计时控件

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值