Implementation of a simple countdown control

I haven't written a blog for a long time. I wanted to be my own knowledge base, but sometimes I can't keep it up. If I have time to update it again today, I will write a simple countdown implementation. In fact, there is no difficulty. Handler+Timer can achieve it.

The principle is: start a timer, use handler to send a request every second, and then go to handleMessage to process, determine the hours, minutes, seconds, and finally display on the page (no particularly difficult to understand, no comment is written in the code, ps: seems to be someone else's reference, but it's a bit too long, forgotten)
Let's go directly to the code below:
Layout file:

<?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>

Code for 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();
    }
}

Tags: Android Java xml encoding

Posted on Tue, 05 May 2020 08:27:26 -0700 by progman