原文章地址:
下面逐一介绍:
- Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- //这里写handler要处理的工作,例如页面的文字的改变,
- }
- };
- public class MyHandlerThread implements Runnable{
- @Override
- public void run() {
- while (true){
- try {
- //线程休眠一秒钟
- Thread.sleep(1000);
- //通过Message对象来发送消息
- Message message = new Message();
- message.what=1;
- handler.sendMessage(message);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- new Thread(new MyHandlerThread()).start();
这个实现比较简单,在实际开发中也经常用到。
- Handler handler = new Handler();
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- //这里写处理的逻辑
- handler.postDelayed(this, 2000);
- }
- };
- 调用 handler.postDelayed(this, 2000);方法,会每隔两秒执行一个runnable中的run()方法,达到定时的效果。
- handler.removeCallbacks(runnable);
- private Timer timer;
- private TimerTask timerTask;
- Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- //这里写要处理的逻辑
- super.handleMessage(msg);
- }
- };
- timerTask = new TimerTask() {
- @Override
- public void run() {
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
- };
- 最后调用timer的schedule()方法来启动定时器。
schedule()如下所示:
- timer.cancel();调用timer的cancle()方法即可关闭该定时器。
项目地址,可以下载原代码来学习:, 下面简要说明一下用法。
这里主要是一个自定义的 View——-RushBuyCountDownTimerView,
- package com.qust.widght;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.os.Handler;
- import android.os.Message;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.qust.rushbuycountdowntimerview.R;
- @SuppressLint("HandlerLeak")
- public class RushBuyCountDownTimerView extends LinearLayout {
- // 小时,十位
- private TextView tv_hour_decade;
- // 小时,个位
- private TextView tv_hour_unit;
- // 分钟,十位
- private TextView tv_min_decade;
- // 分钟,个位
- private TextView tv_min_unit;
- // 秒,十位
- private TextView tv_sec_decade;
- // 秒,个位
- private TextView tv_sec_unit;
- private Context context;
- private int hour_decade;
- private int hour_unit;
- private int min_decade;
- private int min_unit;
- private int sec_decade;
- private int sec_unit;
- // 计时器
- private Timer timer;
- private Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- countDown();
- };
- };
- public RushBuyCountDownTimerView(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.context = context;
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.view_countdowntimer, this);
- tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade);
- tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit);
- tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade);
- tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit);
- tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade);
- tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit);
- }
- /**
- *
- * @Description: 开始计时
- * @param
- * @return void
- * @throws
- */
- public void start() {
- if (timer == null) {
- timer = new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- handler.sendEmptyMessage(0);
- }
- }, 0, 1000);
- }
- }
- /**
- *
- * @Description: 停止计时
- * @param
- * @return void
- * @throws
- */
- public void stop() {
- if (timer != null) {
- timer.cancel();
- timer = null;
- }
- }
- /**
- * @throws Exception
- *
- * @Description: 设置倒计时的时长
- * @param
- * @return void
- * @throws
- */
- public void setTime(int hour, int min, int sec) {
- if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0|| sec < 0) {
- throw new RuntimeException("Time format is error,please check out your code");
- }
- hour_decade = hour / 10;
- hour_unit = hour - hour_decade * 10;
- min_decade = min / 10;
- min_unit = min - min_decade * 10;
- sec_decade = sec / 10;
- sec_unit = sec - sec_decade * 10;
- tv_hour_decade.setText(hour_decade + "");
- tv_hour_unit.setText(hour_unit + "");
- tv_min_decade.setText(min_decade + "");
- tv_min_unit.setText(min_unit + "");
- tv_sec_decade.setText(sec_decade + "");
- tv_sec_unit.setText(sec_unit + "");
- }
- /**
- *
- * @Description: 倒计时
- * @param
- * @return boolean
- * @throws
- */
- private void countDown() {
- if (isCarry4Unit(tv_sec_unit)) {
- if (isCarry4Decade(tv_sec_decade)) {
- if (isCarry4Unit(tv_min_unit)) {
- if (isCarry4Decade(tv_min_decade)) {
- if (isCarry4Unit(tv_hour_unit)) {
- if (isCarry4Decade(tv_hour_decade)) {
- Toast.makeText(context, "时间到了",Toast.LENGTH_SHORT).show();
- stop();
- }
- }
- }
- }
- }
- }
- }
- /**
- *
- * @Description: 变化十位,并判断是否需要进位
- * @param
- * @return boolean
- * @throws
- */
- private boolean isCarry4Decade(TextView tv) {
- int time = Integer.valueOf(tv.getText().toString());
- time = time - 1;
- if (time < 0) {
- time = 5;
- tv.setText(time + "");
- return true;
- } else {
- tv.setText(time + "");
- return false;
- }
- }
- /**
- *
- * @Description: 变化个位,并判断是否需要进位
- * @param
- * @return boolean
- * @throws
- */
- private boolean isCarry4Unit(TextView tv) {
- int time = Integer.valueOf(tv.getText().toString());
- time = time - 1;
- if (time < 0) {
- time = 9;
- tv.setText(time + "");
- return true;
- } else {
- tv.setText(time + "");
- return false;
- }
- }
- }
从上面代码中即可看到,此倒计时效果就是使用上面所提到的第三种方法来完成的,其中所用到的 view_countdowntimer 布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@android:color/white"
- android:orientation="horizontal"
- android:padding="10dp" >
- <TextView
- android:id="@+id/tv_hour_decade"
- style="@style/RushBuyCountDownTimerViewStyle" />
- <TextView
- android:id="@+id/tv_hour_unit"
- style="@style/RushBuyCountDownTimerViewStyle"
- android:layout_marginLeft="1dp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:background="@android:color/white"
- android:gravity="center"
- android:text=":"
- android:textColor="#4F4242"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tv_min_decade"
- style="@style/RushBuyCountDownTimerViewStyle" />
- <TextView
- android:id="@+id/tv_min_unit"
- style="@style/RushBuyCountDownTimerViewStyle"
- android:layout_marginLeft="1dp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:background="@android:color/white"
- android:gravity="center"
- android:text=":"
- android:textColor="#4F4242"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tv_sec_decade"
- style="@style/RushBuyCountDownTimerViewStyle" />
- <TextView
- android:id="@+id/tv_sec_unit"
- style="@style/RushBuyCountDownTimerViewStyle"
- android:layout_marginLeft="1dp" />
- </LinearLayout>
RushBuyCountDownTimerViewStyle 这个 Style 文件如下,在这个文件中主要定义字体大小、颜色、背景色等:
- name="RushBuyCountDownTimerViewStyle">
- <item name="android:layout_width">wrap_content</item>
- <item name="android:layout_height">wrap_content</item>
- <item name="android:background">@drawable/bg_view</item>
- <item name="android:gravity">center</item>
- <item name="android:text">0</item>
- <item name="android:textColor">@android:color/white</item>
- <item name="android:textSize">35sp</item>
- <item name="android:textStyle">bold</item>
上面的布局效果默认是这样的:
- 00:00:00 //分别对应时、分、秒
最后:
假如需要在 MainActivity.java 中使用此倒计时控件,只需要在 MainActivityjava 的布局文件 activity_main.xml 中加入此控件,设置倒计时的时候,然后在代码中调用 start()方法即可实现倒计时的效果。
activity_main.xml 文件如下:
- "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" >
- <com.qust.widght.RushBuyCountDownTimerView
- android:id="@+id/timerView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </com.qust.widght.RushBuyCountDownTimerView>
MainActivity.java 文件如下:
- package com.qust.widght;
- import android.app.Activity;
- import android.os.Bundle;
- import com.qust.rushbuycountdowntimerview.R;
- public class MainActivity extends Activity {
- private RushBuyCountDownTimerView timerView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- timerView = (RushBuyCountDownTimerView) findViewById(R.id.timerView);
- //设置倒计时的时、分、秒
- timerView.setTime(10, 0, 10);
- // 开始倒计时
- timerView.start();
- }
- }
最后:通过上面的几个步骤即可完成倒计时效果,且 rushbuycountdowntimerview 中也加入了倒计时结束时候的逻辑处理,一般我们需要在这里给用户提示信息,或者对 View 进行处理等,详细的就不在这里说了。
拜拜,洗洗睡觉去。。。
欢迎关注我的个人技术公众号, 快速查看我的最新文章。
- 时间:2016-10-17 23:00
来源: