最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購倒計(jì)時(shí)

 更新時(shí)間:2022年06月30日 14:30:20   作者:Tab_Esc  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天項(xiàng)目用到了搶購時(shí)倒計(jì)時(shí)的功能 ,今天找了好多哥們,也百度了好多,但是沒有自己喜歡并且能消化的。好不容易找到了一個(gè)能容易看懂的,然而又不符合我想要的需求,所以自己搞一下。不知道是否簡單,但是最起碼是項(xiàng)目的功能可以實(shí)現(xiàn)。(一下午,沒白費(fèi)。開心)

直接上代碼好了,我相信都能看懂。我除外。(我搞了一下午)哎。反正現(xiàn)在懂點(diǎn)了。。。

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;
 // 天,個(gè)位
 private TextView tv_day_unit;
 // 小時(shí),十位
 private TextView tv_hour_decade;
 // 小時(shí),個(gè)位
 private TextView tv_hour_unit;
 // 分鐘,十位
 private TextView tv_min_decade;
 // 分鐘,個(gè)位
 private TextView tv_min_unit;
 // 秒,十位
 private TextView tv_sec_decade;
 // 秒,個(gè)位
 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;
 // 計(jì)時(shí)器
 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: 開始計(jì)時(shí)
  * @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: 停止計(jì)時(shí)
  * @param
  * @return void
  * @throws
  */
 public void stop() {
  if (timer != null) {
   timer.cancel();
   timer = null;
  }
 }

 // 如果:sum = 12345678
 public void addTime(int sum) {

  // 求出天數(shù)
  int day = sum / 60 / 60 / 24;
  // int day_time = sum % 24;
  Log.e("小時(shí)", day + "");
  Log.e("小時(shí)", sum % 24 + "");

  // 求出小時(shí)
  // int hour = day_time / 60;
  // int hour_time = day_time % 60;
  //
  // Log.e("小時(shí)", hour + "");
  //
  // 先獲取個(gè)秒數(shù)值
  int sec = sum % 60;
  // 如果大于60秒,獲取分鐘。(秒數(shù))
  int sec_time = sum / 60;
  // 再獲取分鐘
  int min = sec_time % 60;
  // 如果大于60分鐘,獲取小時(shí)(分鐘數(shù))。
  int min_time = sec_time / 60;
  // 獲取小時(shí)
  int hour = min_time % 24;
  // 剩下的自然是天數(shù)
  day = min_time / 24;

  //
  // Log.e("分鐘", min + "");
  //
  // // 求出秒數(shù)
  // Log.e("秒數(shù)", sec + "");
  setTime(day, hour, min, sec);

 }

 /**
  * @throws Exception
  * 
  * @Description: 設(shè)置倒計(jì)時(shí)的時(shí)長
  * @param
  * @return void
  * @throws
  */
 public void setTime(int day, int hour, int min, int sec) {
 //這里的天數(shù)不寫也行,我寫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 的十位數(shù)
  day_decade = day / 10;
  // day的個(gè)位數(shù),這里求余就行
  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;
  // 第個(gè)time 進(jìn)行初始化
  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: 倒計(jì)時(shí)
  * @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, "時(shí)間到了",
            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;
 }

 /**
  * 進(jìn)行——時(shí)分秒,判斷個(gè)位數(shù)
  * 
  * @Description: 變化十位,并判斷是否需要進(jìn)位
  * @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;
  }

 }

 /**
  * 進(jìn)行——時(shí)分秒,判斷個(gè)位數(shù)
  * 
  * @Description: 變化個(gè)位,并判斷是否需要進(jìn)位
  * @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;
  }

 }

 /**
  * 進(jìn)行——時(shí)分秒,判斷個(gè)位數(shù)
  * 
  * @Description: 變化十位,并判斷是否需要進(jìn)位
  * @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;
  }

 }

 /**
  * 進(jìn)行——時(shí)分秒,判斷個(gè)位數(shù)
  * 
  * @Description: 變化個(gè)位,并判斷是否需要進(jìn)位
  * @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;
  }

 }

}

主類進(jìn)行調(diào)用

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);
  // 設(shè)置時(shí)間(day,hour,min,sec)
  // timerView.setTime(0, 0, 0, 5);
  int sum = 60;
  // 把秒數(shù)傳到倒計(jì)時(shí)方法中。。
  timerView.addTime(sum);
  // 開始倒計(jì)時(shí)
  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"
  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>

希望可以幫到有需求的人。同時(shí)以后自己忘記時(shí)也能看一眼。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android圖片處理之讓圖片一直勻速旋轉(zhuǎn)

    android圖片處理之讓圖片一直勻速旋轉(zhuǎn)

    讓圖片一直勻速旋,這篇文章主要介紹了android圖片處理之讓圖片一直勻速旋轉(zhuǎn)的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Flutter?Widget之FutureBuilder使用示例詳解

    Flutter?Widget之FutureBuilder使用示例詳解

    這篇文章主要為大家介紹了Flutter?Widget之FutureBuilder使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android編程簡單解析JSON格式數(shù)據(jù)的方法示例

    Android編程簡單解析JSON格式數(shù)據(jù)的方法示例

    這篇文章主要介紹了Android編程簡單解析JSON格式數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android編程解析json格式數(shù)據(jù)的實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android Compose實(shí)現(xiàn)伸縮ToolBar的思路詳解

    Android Compose實(shí)現(xiàn)伸縮ToolBar的思路詳解

    這篇文章主要介紹了Android Compose之伸縮ToolBar的實(shí)現(xiàn),本文給大家分享主要實(shí)現(xiàn)思路及實(shí)現(xiàn)過程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Android自定義Seekbar滑動條 Pop提示跟隨滑動按鈕滑動

    Android自定義Seekbar滑動條 Pop提示跟隨滑動按鈕滑動

    這篇文章主要為大家詳細(xì)介紹了Android自定義Seekbar滑動條,Pop提示跟隨滑動按鈕滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • android 權(quán)限大全 分享

    android 權(quán)限大全 分享

    今天上課老師提問訪問權(quán)限,好多都沒答上來,特意收集整理了放上來
    2013-06-06
  • Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    這篇文章主要介紹了Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法,結(jié)合實(shí)例形式分析了Android頁面之間進(jìn)行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法

    Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法,結(jié)合具體實(shí)例形式分析了Android使用Gallery實(shí)現(xiàn)畫廊功能的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • Android實(shí)現(xiàn)一對一藍(lán)牙聊天APP

    Android實(shí)現(xiàn)一對一藍(lán)牙聊天APP

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一對一藍(lán)牙聊天APP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android自定義圓角柱狀圖

    Android自定義圓角柱狀圖

    這篇文章主要為大家詳細(xì)介紹了Android自定義圓角柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論

龙州县| 永顺县| 盐池县| 玉田县| 梧州市| 拜泉县| 马龙县| 玛沁县| 四平市| 苍山县| 连州市| 永德县| 阜城县| 应用必备| 玉林市| 揭西县| 鄂伦春自治旗| 天气| 中西区| 秀山| 思茅市| 福州市| 宁夏| 根河市| 顺义区| 温宿县| 百色市| 山丹县| 宜丰县| 策勒县| 历史| 巴彦县| 冷水江市| 鸡泽县| 仪征市| 雅江县| 北辰区| 沧源| 泰州市| 建宁县| 广丰县|