android倒计时控件

1、自定义Textview类

package utils;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimerTextView extends TextView implements Runnable {
    public static String Ltime = null;

    public TimerTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    private long mmin, msecond;
    public boolean run = false;

    public void setTimes(long[] times) {
        mmin = times[0];
        msecond = times[1];

    }

    private void ComputeTime() {
        msecond--;
        if (msecond < 0) {
            mmin--;
            msecond = 59;
        }
    }

    public boolean isRun() {
        return run;
    }
//开始倒计时
    public void beginRun() {
        this.run = true;
        run();
    }
//暂停操作
    public void stopRun() {
        this.run = false;
        this.removeCallbacks(this);//暂停时移除当前线程,否则会加快运行
    }

//多线程,时间按mm:ss格式显示
    @Override
    public void run() {
            if (this.run) {
                ComputeTime();
                if (mmin > 9 && msecond > 9) {
                    String strTime = mmin + ":" + msecond;
                    this.setText(strTime);
                } else if (mmin > 9 && msecond < 10) {
                    String strTime = mmin + ":0" + msecond;
                    this.setText(strTime);
                } else if (mmin < 10 && msecond > 9) {
                    String strTime = "0" + mmin + ":" + msecond;
                    this.setText(strTime);
                } else if (mmin < 10 && msecond < 10) {
                    String strTime = "0" + mmin + ":0" + msecond;
                    this.setText(strTime);
                }
                this.postDelayed(this, 1000);
            }
            if (mmin == 0 && msecond == 0) {
                stopRun();
            }
    }
//剩余时间的分和秒
    public long getRemainMinute() {
        return mmin;
    }
    public long getRemainSecond(){
        return msecond;
    }
}

2、MainActivity

package com.harvic.trytimerview;
/**
 * @author harvic
 * @address blog.csdn.net/harvic880925
 * @date 2014-12-17
 */
import java.util.Calendar;

import com.example.trytimerview.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化倒计时控件
final TimerTextView timerTextView = (TimerTextView)findViewById(R.id.timer_text_view);
long[] times = {0,10,5,30};
timerTextView.setTimes(times);


        Button startBtn =  (Button)findViewById(R.id.main_start_btn);
        Button stopBtn  =  (Button)findViewById(R.id.main_stop_btn);
        //开始倒计时
        startBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(!timerTextView.isRun()){
                    timerTextView.beginRun();
                }
            }
        });

        //停止倒计时
        stopBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(timerTextView.isRun()){
                    timerTextView.stopRun();
                }
            }
        });

    }

}

3、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.harvic.trytimerview.MainActivity" >

    <Button android:id="@+id/main_start_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start run"/>

    <Button android:id="@+id/main_stop_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stop run"/>


    <com.harvic.trytimerview.TimerTextView 
        android:id="@+id/timer_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#ff0000"
        android:gravity="center_horizontal"
        android:text="倒计时"
        />

</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值