Andorid realizes snapping countdown accurate to days minutes and seconds
- 2021-08-21 21:29:24
- OfStack
Today, the project used the countdown function when snapping up. Today, I found a lot of buddies and Baidu, but I didn't like it and can digest it. I finally found one that can be easily understood, but it doesn't meet the needs I want, so I made it myself. I don't know if it is simple, but at least the function of the project can be realized. (1 afternoon, not in vain. Happy)
Go directly to the code, I believe you can understand it. Except for me. (I've been doing it all afternoon) Hey. Anyway, I understand something now. . .
package com.qust.widght;
import java.text.SimpleDateFormat;
import java.util.Date;
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.util.Log;
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 {
// God, 10 Bit
private TextView tv_day_decade;
// Days, bits
private TextView tv_day_unit;
// Hours, 10 Bit
private TextView tv_hour_decade;
// Hours, bits
private TextView tv_hour_unit;
// Minutes, 10 Bit
private TextView tv_min_decade;
// Minutes, bits
private TextView tv_min_unit;
// Seconds, 10 Bit
private TextView tv_sec_decade;
// Seconds, bits
private TextView tv_sec_unit;
private Context context;
private int day_decade;
private int day_unit;
private int hour_decade;
private int hour_unit;
private int min_decade;
private int min_unit;
private int sec_decade;
private int sec_unit;
// Timer
private Timer timer;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
countDown();
};
};
private int day = 0;
private int hour = 0;
private int min = 0;
private int sec = 0;
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_day_decade = (TextView) view.findViewById(R.id.tv_day_decade);
tv_day_unit = (TextView) view.findViewById(R.id.tv_day_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: Start timing
* @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: Stop timing
* @param
* @return void
* @throws
*/
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
// If :sum = 12345678
public void addTime(int sum) {
// Calculate the number of days
int day = sum / 60 / 60 / 24;
// int day_time = sum % 24;
Log.e(" Hours ", day + "");
Log.e(" Hours ", sum % 24 + "");
// Calculate the hour
// int hour = day_time / 60;
// int hour_time = day_time % 60;
//
// Log.e(" Hours ", hour + "");
//
// Get the number of seconds first
int sec = sum % 60;
// If greater than 60 Seconds, get minutes. (Seconds)
int sec_time = sum / 60;
// Get another minute
int min = sec_time % 60;
// If greater than 60 Minutes, get hours (minutes).
int min_time = sec_time / 60;
// Acquisition hour
int hour = min_time % 24;
// The rest is naturally the number of days
day = min_time / 24;
//
// Log.e(" Minutes ", min + "");
//
// // Find the number of seconds
// Log.e(" Number of seconds ", sec + "");
setTime(day, hour, min, sec);
}
/**
* @throws Exception
*
* @Description: Set the length of the countdown
* @param
* @return void
* @throws
*/
public void setTime(int day, int hour, int min, int sec) {
// I can write the number of days here without writing it. I'll write it 365
if (day >= 365 || hour >= 24 || min >= 60 || sec >= 60 || day < 0
|| hour < 0 || min < 0 || sec < 0) {
throw new RuntimeException(
"Time format is error,please check out your code");
}
// day Adj. 10 Number of digits
day_decade = day / 10;
// day Single digits of , Just ask for the surplus here
day_unit = day - day_decade * 10;
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;
// The first time Initialize
timeClean();
}
private void timeClean() {
tv_day_decade.setText(day_decade + "");
tv_day_unit.setText(day_unit + "");
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: Countdown
* @param
* @return boolean
* @throws
*/
public Boolean countDown() {
if (isCarry4Unit(tv_sec_unit)) {
if (isCarry4Decade(tv_sec_decade)) {
if (isCarry4Unit(tv_min_unit)) {
if (isCarry4Decade(tv_min_decade)) {
if (isDay4Unit(tv_hour_unit)) {
if (isDay4Decade(tv_hour_decade)) {
if (isDay4Unit(tv_day_unit)) {
if (isDay4Decade(tv_day_decade)) {
Toast.makeText(context, " Time is up ",
Toast.LENGTH_SHORT).show();
tv_day_decade.setText("0");
tv_day_unit.setText("0");
tv_hour_decade.setText("0");
tv_hour_unit.setText("0");
tv_min_decade.setText("0");
tv_min_unit.setText("0");
tv_sec_decade.setText("0");
tv_sec_unit.setText("0");
stop();
return false;
}
}
}
}
}
}
}
}
return false;
}
/**
* Go on-minutes and seconds, judge single digits
*
* @Description: Change 10 Bit and determine whether carry is required
* @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;
}
}
/**
* Go on-minutes and seconds, judge single digits
*
* @Description: Change bits and determine whether carry is needed
* @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;
}
}
/**
* Go on-minutes and seconds, judge single digits
*
* @Description: Change 10 Bit and determine whether carry is required
* @param
* @return boolean
* @throws
*/
private boolean isDay4Unit(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 3;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
/**
* Go on-minutes and seconds, judge single digits
*
* @Description: Change bits and determine whether carry is needed
* @param
* @return boolean
* @throws
*/
private boolean isDay4Decade(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 2;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
}
The main class is called
package com.qust.widght;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
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);
// Setting time (day,hour,min,sec)
// timerView.setTime(0, 0, 0, 5);
int sum = 60;
// Pass the number of seconds to the countdown method. .
timerView.addTime(sum);
// Start the countdown
timerView.start();
}
}
Layout file
<LinearLayout 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"
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>
</LinearLayout>
<?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_day_decade"
style="@style/RushBuyCountDownTimerViewStyle" />
<TextView
android:id="@+id/tv_day_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_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>
I hope I can help those in need. At the same time, I can also see one eye when I forget it in the future.