您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Andorid如何實現精確到天時分秒的搶購倒計時的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
示例:
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 { // 天,十位 private TextView tv_day_decade; // 天,個位 private TextView tv_day_unit; // 小時,十位 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 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; // 計時器 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: 開始計時 * @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; } } // 如果:sum = 12345678 public void addTime(int sum) { // 求出天數 int day = sum / 60 / 60 / 24; // int day_time = sum % 24; Log.e("小時", day + ""); Log.e("小時", sum % 24 + ""); // 求出小時 // int hour = day_time / 60; // int hour_time = day_time % 60; // // Log.e("小時", hour + ""); // // 先獲取個秒數值 int sec = sum % 60; // 如果大于60秒,獲取分鐘。(秒數) int sec_time = sum / 60; // 再獲取分鐘 int min = sec_time % 60; // 如果大于60分鐘,獲取小時(分鐘數)。 int min_time = sec_time / 60; // 獲取小時 int hour = min_time % 24; // 剩下的自然是天數 day = min_time / 24; // // Log.e("分鐘", min + ""); // // // 求出秒數 // Log.e("秒數", sec + ""); setTime(day, hour, min, sec); } /** * @throws Exception * * @Description: 設置倒計時的時長 * @param * @return void * @throws */ public void setTime(int day, int hour, int min, int sec) { //這里的天數不寫也行,我寫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 的十位數 day_decade = day / 10; // day的個位數,這里求余就行 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; // 第個time 進行初始化 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: 倒計時 * @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, "時間到了", 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; } /** * 進行——時分秒,判斷個位數 * * @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; } } /** * 進行——時分秒,判斷個位數 * * @Description: 變化十位,并判斷是否需要進位 * @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; } } /** * 進行——時分秒,判斷個位數 * * @Description: 變化個位,并判斷是否需要進位 * @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; } } }
主類進行調用
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); // 設置時間(day,hour,min,sec) // timerView.setTime(0, 0, 0, 5); int sum = 60; // 把秒數傳到倒計時方法中。。 timerView.addTime(sum); // 開始倒計時 timerView.start(); } }
布局文件
<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" /> <TextView android:id="@+id/tv_day_unit" 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" /> <TextView android:id="@+id/tv_hour_unit" 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" /> <TextView android:id="@+id/tv_min_unit" 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" /> <TextView android:id="@+id/tv_sec_unit" android:layout_marginLeft="1dp" /> </LinearLayout>
感謝各位的閱讀!關于“Andorid如何實現精確到天時分秒的搶購倒計時”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。