倒计时器

XML文件

<LinearLayout
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_gravity="center_horizontal"
    android:layout_width="300dp"
    android:layout_height="76dp">
    <!-- -->
    <LinearLayout
        android:layout_marginLeft="40dp"
        android:background="@mipmap/timebac"
        android:orientation="vertical"
        android:layout_width="50dp"
        android:layout_height="70dp">
        <TextView
            android:id="@+id/day_subject_time"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:text="22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <!--分隔符 -->
    <ImageView
        android:layout_marginLeft="10dp"
        android:layout_marginTop="13dp"
        android:src="@mipmap/point"
        android:layout_width="20dp"
        android:layout_height="40dp" />
    <LinearLayout
        android:layout_marginLeft="10dp"
        android:background="@mipmap/timebac"
        android:orientation="vertical"
        android:layout_width="50dp"
        android:layout_height="70dp">
        <TextView
            android:id="@+id/day_subject_minuts"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:text="39"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <!--分隔符 -->
    <ImageView
        android:layout_marginLeft="10dp"
        android:layout_marginTop="13dp"
        android:src="@mipmap/point"
        android:layout_width="20dp"
        android:layout_height="40dp" />
    <LinearLayout
        android:layout_marginLeft="10dp"
        android:background="@mipmap/timebac"
        android:orientation="vertical"
        android:layout_width="50dp"
        android:layout_height="70dp">
        <TextView
            android:id="@+id/day_subject_second"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:text="22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="center_horizontal"
            android:textColor="#ff4c4c"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
Activity

Timer timer=new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        Message message = new Message();
        message.what = 1;

        handler.sendMessage(message);
    }
};
Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        switch (msg.what) {
            case 1:
                seconds=seconds-1;
                if(seconds<0){
                    minutess=minutess-1;
                    seconds=59;
                    if(minutess<=0){
                        hourss=hourss-1;
                        minutess=59;
                        if(hourss<0){
                            hourss=0;
                            minutess=0;
                            seconds=0;                           
                        }
                    }
                }
                setViewData(hourss,minutess,seconds);            
        }
    }
};
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_day_subject); 
    int[] showTime = shouldShowTime(Constants.DAILY_QUESTION_TIME);
    hourss=showTime[0];
    minutess=showTime[1];
    seconds=showTime[2];
initView(); setViewData(hourss, minutess, seconds); timer.schedule(task, 1000, 1000);}
private void setViewData(int hou,int minit,int seconds){
    txt_time.setText(hou + "");
    txt_minutes.setText(minit + "");
    txt_seconds.setText(seconds + "");
}
private void initView() {
    //    txt_time = (TextView)findViewById(R.id.day_subject_time);
    //    txt_minutes = (TextView)findViewById(R.id.day_subject_minuts);
    //    txt_seconds = (TextView)findViewById(R.id.day_subject_second);
}
private int[] shouldShowTime(String s) {
    //更新的时间
    String[] changeTime = initChangeData(s);
    //当前的时间
    String[] currentTime = initCurrentData();
    //更新时间,时,分,秒
    String changeSh=changeTime[0];
    String changeMi=changeTime[1];
    String changeSe=changeTime[2];
    //当前时间,时,分,秒
    String currentSh=currentTime[0];
    String currentMi=currentTime[1];
    String currentSe=currentTime[2];
    //应该显示的时间
    int[] ints = toTime(toSeconds(changeTime, currentTime));
    return ints;


}
//转换成秒
private Long toSeconds(String[] changeTime , String[] currentTime) {
    Long ret=null;
    //更新时间,时,分,秒
    String changeSh=changeTime[0];
    String changeMi=changeTime[1];
    String changeSe=changeTime[2];
    long chanLong=Long.parseLong(changeSh)*3600+Long.parseLong(changeMi)*60+Long.parseLong(changeSe);
    //当前时间,时,分,秒
    String currentSh=currentTime[0];
    String currentMi=currentTime[1];
    String currentSe=currentTime[2];
    long currentLong=Long.parseLong(currentSh)*3600+Long.parseLong(currentMi)*60+Long.parseLong(currentSe);
    if(chanLong>=currentLong){
        ret = chanLong-currentLong;
    }else{
        ret= 24*3600-(currentLong-chanLong);
    }
    return ret;
}
//转换成时间
private int[] toTime(Long seconds){
    int[] ret=new int[3];
    ret[0]=(int)(seconds/3600);
    ret[1]=(int)((seconds / 60) % 60);
    ret[2]=(int)(seconds%60);

    return ret;
}

private String[] initChangeData(String s) {
    String[] ret=null;
    ret = s.split(":");
    return ret;

}
private String[] initCurrentData( ) {
    String[] ret=null;

    SimpleDateFormat formatter    =   new    SimpleDateFormat    ("HH:mm:ss");
    Date curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
    String    str    =    formatter.format(curDate);
    ret = str.split(":");
    return ret;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值