您的位置:首页 > 移动开发 > Android开发

android自定义倒计时控件示例

2014-01-26 14:27 1076 查看
import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import android.widget.TextView;

import com.new0315.R;/** * 自定义倒计时文本控件 * @author Administrator * */public class TimeTextView extends TextView implements Runnable{

    Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息

    private long[] times;

    private long mday, mhour, mmin, msecond;//天,小时,分钟,秒

    private boolean run=false; //是否启动了

    public TimeTextView(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint=new Paint();        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响    }

    public TimeTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        mPaint=new Paint();        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响    }

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

    public long[] getTimes() {        return times;    }

    public void setTimes(long[] times) {        this.times = times;        mday = times[0];        mhour = times[1];        mmin = times[2];        msecond = times[3];

    }

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

                }            }

        }

    }

    public boolean isRun() {        return run;    }

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

    @Override    public void run() {        //标示已经启动        run=true;

        ComputeTime();

        String strTime="还剩</pre><span style="color: red;">"+mday+"</span><pre>"+"天</pre><span style="color: red;">"+mhour+"</span><pre>小时</pre><span style="color: red;">"+ mmin+"</span><pre>分钟</pre><span style="color: red;">"+msecond+"</span><pre>秒";        this.setText(Html.fromHtml(strTime));

        postDelayed(this, 1000);

    }

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 倒计时