好久没有写博客了,本来想当做自己的知识库来着,但是有时候不能坚持下来,今天有时间就再来更新下,就写一个简单的倒计时实现,其实没啥难点,Handler+Timer就可以实现.
原理就是:启动一个定时器,每个1秒钟利用handler发送一个请求,然后到handleMessage处理,判断小时,分钟,秒是多少,最后显示到页面上(没有特别难理解的,代码里面就没有写注释,ps:貌似好像也是借鉴的别人的,但是时间有点久远,忘记了)
下面直接上代码吧:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.jiuair.booking.activity.Djs">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
<TextView
android:id="@+id/tv_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_special_tv"
android:gravity="center"
android:text="48"
android:textColor="@color/white"
android:textSize="11sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="2dp"
android:text="h"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="@drawable/icon_special_tv"
android:gravity="center"
android:text="48"
android:textColor="@color/white"
android:textSize="11sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="2dp"
android:text="m"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="@drawable/icon_special_tv"
android:gravity="center"
android:text="48"
android:textColor="@color/white"
android:textSize="11sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="2dp"
android:text="s"
android:textColor="@color/black" />
</LinearLayout>
</RelativeLayout>
activity的代码:
package com.jiuair.booking.activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jiuair.booking.R;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class Djs extends AppCompatActivity {
private Handler handler;
private int hour;
private int min;
private int second;
private LinearLayout ll_package;
private TextView tv_hour;
private TextView tv_second;
private TextView tv_min;
private Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_djs);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
computeTime();
if (hour <= 0 && min <= 0 && second < 0){
if (timer != null){
timer.cancel();
}
handler.removeCallbacks(null);
}else{
tv_hour.setText(hour+"");
tv_min.setText(min+"");
tv_second.setText(second+"");
}
}
};
tv_hour = (TextView)findViewById(R.id.tv_hour);
tv_min = (TextView)findViewById(R.id.tv_min);
tv_second = (TextView)findViewById(R.id.tv_second);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(10);
hour = list.get(0);
min = list.get(1);
second = list.get(2);
tv_hour.setText(hour+"");
tv_min.setText(min+"");
tv_second.setText(second+"");
timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
handler.sendMessage(message);
}
},100,1000);
}
private void computeTime() {
second--;
if (second < 0) {
min--;
second = 59;
if (min < 0) {
min = 59;
hour--;
}
}
}
@Override
public void onDestroy() {
handler.removeCallbacks(null);
if (timer != null){
timer.cancel();
}
super.onDestroy();
}
}