自定义控件----倒计时控件

先上效果图:

因为时分秒都有自己的背景色等布局,所以重写一个textview 不够灵活,所以我们自定义一个TimeTextView继承自Linearlayout 然后再在里面放几个textview即可。

先看 布局文件吧:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="@dimen/default_margin"
              android:layout_marginLeft="@dimen/default_margin"
              android:layout_marginRight="@dimen/default_margin"
              android:background="@android:color/transparent"
              android:gravity="center"
              android:orientation="horizontal"
              android:padding="@dimen/default_margin_small">

    <TextView
        android:id="@+id/tv_hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="24"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":"
        android:textColor="@color/default_text_red"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:id="@+id/tv_minutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":"
        android:textColor="@color/default_text_red"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:id="@+id/tv_seconds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>
</LinearLayout>

然后就是自定义的linearlayout了,

/**
 * 倒计时 3文本
 * Created By Fangchao On 2015/3/12
 */
public class TimeTextView extends LinearLayout {
    private long mday, mhour, mmin, msecond;//天,小时,分钟,秒
    private boolean run = false; //是否启动了
    Timer timer = new Timer();
    TextView Vhour, Vmin, Vseconds;

    public TimeTextView(Context context) {
        super(context);
        iniUI(context);
    }

    public TimeTextView(Context context, AttributeSet attrs) {

        super(context, attrs);
        iniUI(context);
    }

    public void iniUI(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View myView = mInflater.inflate(R.layout.view_time_texviews, null);

        Vhour = (TextView) myView.findViewById(R.id.tv_hours);
        Vmin = (TextView) myView.findViewById(R.id.tv_minutes);
        Vseconds = (TextView) myView.findViewById(R.id.tv_seconds);
        addView(myView);
    }

    public TimeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        iniUI(context);
    }

    private Handler mHandler = new Handler() {

    };

    public boolean isRun() {
        return run;
    }

    public void setRun(boolean run) {
        this.run = run;
    }

    public void start() {
        if (!isRun()) {
            setRun(true);
            timer.schedule(task, 1000, 1000);
        }
    }

    /**
     * 根据传进来的时间差 为textview 赋值
     *
     * @param duration
     */
    public void setTimes(long duration) {
        Date date = new Date(duration);
        Date date1 = new Date(1L);
        /*mday = duration / 60000 / 60 / 24;
        mhour = (duration - mday * 6000 * 60 * 24) / 3600000;

        mmin = (duration - mhour * 6000 * 60 - mday * 3600000 * 24) / 60000;
        msecond = (duration - mmin * 60000 - mhour * 3600000 - mday * 3600000 * 24) / 60000;*/
//需要修改,测试用
        mday = date.getDay();
        mhour = date.getHours();
        mmin = date.getMinutes();
        msecond = date.getSeconds();
    }

    /**
     * 倒计时计算
     */
    private void ComputeTime() {
        msecond--;
        if (msecond < 0) {
            mmin--;
            msecond = 59;
            if (mmin < 0) {
                mmin = 59;
                mhour--;
                if (mhour < 0) {
                    // 倒计时结束
                    mhour = 24;
                    mday--;
                }
            }
        }
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            mHandler.post(new Runnable() {      // UI thread
                @Override
                public void run() {
                    run = true;
                    ComputeTime();
                    if (mday < 0) {
                        setVisibility(View.GONE);

                        setRun(false);
                    }
                    Vhour.setText(mhour < 10 ? ("0" + mhour) : mhour + "");
                    Vseconds.setText(msecond < 10 ? ("0" + msecond) : msecond + "");
                    Vmin.setText(mmin < 10 ? ("0" + mmin) : mmin + "");
                }
            });
        }
    };
}

用的时候直接在需要的地方直接把TimeTextview 放上就行了,

举个例子吧,可以在adapter中设置倒计时时长。。。。

   @Override
    public void onBindHeaderView(RecyclerView.ViewHolder holder, int position) {
        HeaderViewHolder headViewHolder = (HeaderViewHolder) holder;

        long duration=  1426244976513L + 200 * 1000 - TimeUtils.getCurrentTimeInLong();
        Log.e("long///////", TimeUtils.getCurrentTimeInLong() + "");
        if (!headViewHolder.timeTextView.isRun()) {

            headViewHolder.timeTextView.setTimes(duration);
            headViewHolder.timeTextView.start();
        }
    }
时间: 2024-08-05 20:08:16

自定义控件----倒计时控件的相关文章

Android自定义倒计时控件

序: 最近越来越多的APP都是用手机号注册,一是为了方便用户记忆,二是为了增加用户账户的安全性.在我们进行交易操作或者修改密码等操作的时候,这时候需要输入短信验证码.这个控件会需要有倒计时的功能,这里主要总结常见的几种实现方式. 1.Android中实现倒计时的方法 第一种:直接用Handler的消息机制来实现 这种方式感觉是最原始的,这里不多说. 第二种:Timer和TimerTask 基本使用:获得Timer和TimerTask对象,然后启动,倒计时的逻辑写在handler里面 privat

android自定义倒计时控件示例

这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧 自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import and

winfrom 倒计时控件

最近在做一个快递柜项目,要求在用户没有操作的时间到了一分钟,自动返回主页,我于是封装了一个倒计时控件,废话少说,直接上代码 public partial class RemainingTimeUC : UserControl { public RemainingTimeUC() { InitializeComponent(); } Form ParantForm = null; private void RemainingTimeUC_Load(object sender, EventArgs

android中倒计时控件CountDownTimer分析

android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { LogUtil.i(TAG, "seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { LogUtil.i(TAG, "done!"

倒计时控件

最近做一个WPF小项目需要使用到计时器,因此写了一个计时控件,记录下来,以便下次使用. 前台的XAML: <UserControl x:Class="Test.CountDown" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc=&

自定义控件---系统控件组合式(案例二)

-----------------------------------------------案例效果------------------------------------- activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout

模仿淘宝客户端倒计时控件

在前面的文章中,我们分析了淘宝android客户端的一些界面时间和用户体验,今天这篇文章,主要介绍如何使用自定义控件,实现抢购倒计时的功能. 首先,我们看一下实现的效果. 实现效果很简单哈,就是一个倒计时的自定义控件. 下面简单介绍一下实现的思路. 首先,显示时间使用的是Textview,因为没有很特殊的效果,因此,我们可以自己写一个简单的布局文件,来作为显示的界面. 而关于时间的变更,我们使用timer类就可以实现,用一个1000毫秒的Timer,每过一秒,更新一下界面即可. 但是在更新时间的

自定义控件---系统控件组合式(案例一)

-----------------------------案例一---------------动画效果不截图了------------------------------------- activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:la

自定义控件---系统控件组合式(案例三)

效果图如下 -------------------------------------------------- activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa