Android自定义60s倒计时控件 添加实时监听

来源:互联网 发布:三维立体图软件 编辑:程序博客网 时间:2024/06/06 04:44

简述 
倒计时控件在项目中,这里,我写的是关于等待一分钟的倒计时。

思路 
自定义TimeCountDown继承TextView,初始化倒计时时间60s(其实自定义控件我们一般还需要写入attr文件,但是这里就没有自己写,因为我这里所需只要求60s,所以没有扩展)我们每1秒钟,发送一个消息,在handleMessage()中:令count = count-1,倒计时时间不断递减,并显示。

自定义 view

public class TimeCountDown extends TextView {    private static final String TAG = TimeCountDown.class.getSimpleName();    private static final int COUNT_DOWN_START = 1;//开始计时    private static final int COUNT_DOWN_LOADING = 2;//计数中    private static final int COUNT_DOWN_FINISH = 3;//计数完成    private static final int COUNT_DOWN_ERROR = 4;//计数出错    /**     * 倒计时时间为60 s     */    private int countTime = 60;    /**     * 变量,用来计数当前倒计时的时间     */    private int mCount;    /**     * 倒计时之前显示的文字     */    private String beforeCount = "60秒";    /**     * 倒计时之后显示的文字     */    private String afterCount = "秒";    /**     * handler     */    private Handler mHandler;    private Timer mTimer;    private TimerTask mTimerTask;    private OnTimerCountDownListener onTimerCountDownListener;    public TimeCountDown(Context context) {        super(context);        init();    }    public TimeCountDown(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public void init() {        mCount = countTime;        setText(beforeCount);        mHandler = new Handler() {            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                switch (msg.what) {                    case COUNT_DOWN_START:                        if (null != onTimerCountDownListener) {                            onTimerCountDownListener.onCountDownStart();                        }                        mCount--;                        break;                    case COUNT_DOWN_LOADING:                        if (null != onTimerCountDownListener) {                            onTimerCountDownListener.onCountDownLoading(mCount);                        }                        setText(msg.arg1 + afterCount);                        mCount--;                        break;                    case COUNT_DOWN_FINISH:                        if (null != onTimerCountDownListener) {                            onTimerCountDownListener.onCountDownFinish();                        }                        if (mCount < 0) {                            //当倒计时时间小于0后,取消计时任务                            mTimer.cancel();                            setText(beforeCount);                            mCount = countTime;                        }                        break;                    case COUNT_DOWN_ERROR:                        if (null != onTimerCountDownListener) {                            onTimerCountDownListener.onCountDownError();                        }                        break;                }            }        };    }    public void initTimer() {        mTimer = new Timer();        mTimerTask = new TimerTask() {            @Override            public void run() {                Log.d(TAG, "当前count:" + mCount);                switch (mCount) {                    case 60:                        sendMessage(COUNT_DOWN_START, 0);                        break;                    case -1:                        sendMessage(COUNT_DOWN_FINISH, 0);                        break;                    default:                        sendMessage(COUNT_DOWN_LOADING, mCount);                        break;                }            }        };        //每隔1秒发送一次空消息        mTimer.schedule(mTimerTask, 0, 1000);    }    private void sendMessage(int flag, int factor) {        Message msg = new Message();        switch (flag) {            case COUNT_DOWN_ERROR:            case COUNT_DOWN_FINISH:            case COUNT_DOWN_START:                break;            case COUNT_DOWN_LOADING:// 计数中                msg.arg1 = factor;                break;            default:                break;        }        msg.what = flag;        mHandler.sendMessage(msg);    }    //这个必须要写,在退出界面时,要取消计时任务,不然,会一直在计时,直到为0.    public void cancel() {        mTimer.cancel();    }    public void setOnTimerCountDownListener(OnTimerCountDownListener listener) {        this.onTimerCountDownListener = listener;    }    public interface OnTimerCountDownListener {        /**         * 计时开始         */        public void onCountDownStart();        /**         * 计时中         */        public void onCountDownLoading(int currentCount);        /**         * 计时错误失败         */        public void onCountDownError();        /**         * 计时完成         */        public void onCountDownFinish();    }}


调用代码

public class LoginActivity extends BaseActivity implements TimeCountDown.OnTimerCountDownListener {    private static final String TAG = LoginActivity.class.getSimpleName();    @InjectView(R.id.back)    LinearLayout back;    @InjectView(R.id.countDown)    TimeCountDown countDown;    @InjectView(R.id.timer)    LinearLayout timer;    @InjectInit    private void init() {        ButterKnife.inject(this);        countDown.setOnTimerCountDownListener(this);        countDown.initTimer();    }    @OnClick(R.id.back)    public void onClick() {        onBackPressed();    }    @Override    protected void onDestroy() {        super.onDestroy();        countDown.cancel();    }    @Override    public void onCountDownStart() {        Log.d(TAG, "onCountDownStart");    }    @Override    public void onCountDownLoading(int currentCount) {        Log.d(TAG, "currentCount:" + currentCount);    }    @Override    public void onCountDownError() {    }    @Override    public void onCountDownFinish() {        Log.d(TAG, "onCountDownFinish");    }}


这个自定义view 我实现的时候,添加了一些监听,是我项目的时候所需要的。可以对计数的控件随时进行暂停、开始、当前状态的一个得知。从而满足自己所需功能

0 0
原创粉丝点击