欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Android實(shí)現(xiàn)京東秒殺功能詳解

 更新時(shí)間:2022年01月26日 11:26:38   作者:路宇  
大家好,本篇文章主要講的是用Android實(shí)現(xiàn)京東秒殺功能詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下

首先看效果圖:

在這里插入圖片描述

京東秒殺是兩個(gè)小時(shí)一個(gè)場(chǎng)次,我們獲取到場(chǎng)次后,通過場(chǎng)次+兩個(gè)小時(shí)后,獲取到最終的時(shí)間,拿最終時(shí)間的時(shí)間戳,與當(dāng)前時(shí)間時(shí)間戳相減,求得剩余的小時(shí),分鐘,秒數(shù),即可實(shí)現(xiàn)倒計(jì)時(shí)功能!

具體代碼實(shí)現(xiàn)如下:

1.布局頁面activity_seckill.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SeckillActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:text="仿京東秒殺"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_screening"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="幾點(diǎn)場(chǎng):"
            android:textColor="@color/black"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_hours"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"
            android:textColor="#fd5343"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_minutes"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"
            android:textColor="#fd5343"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_second"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

2.文本的背景文件為time_back.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fd5343" />
    <corners android:radius="10dp" />
</shape>

3.SeckillActivity類,具體注釋已經(jīng)在代碼中給出

public class SeckillActivity extends AppCompatActivity {
    //秒殺場(chǎng)次
    private TextView tv_screening;
    //2個(gè)小時(shí)一個(gè)秒殺場(chǎng)次,距離秒殺結(jié)束剩余多少小時(shí)
    private TextView tv_hours;
    //剩余分鐘數(shù)
    private TextView tv_minutes;
    //剩余秒數(shù)
    private TextView tv_second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seckill);
        tv_screening = findViewById(R.id.tv_screening);
        tv_hours = findViewById(R.id.tv_hours);
        tv_minutes = findViewById(R.id.tv_minutes);
        tv_second = findViewById(R.id.tv_second);

        //計(jì)算秒殺場(chǎng)次,兩個(gè)小時(shí)一個(gè)場(chǎng)次
        handler.sendEmptyMessage(0x00);
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0x00) {
                //設(shè)置時(shí)間
                mkTime();
            }

            handler.sendEmptyMessageDelayed(0x00, 1000);
            return true;
        }
    });

    private void mkTime() {
        try {
            //使用給定的模式解析日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            StringBuilder stringBuilder = new StringBuilder();
            String format = sdf.format(new Date());
            //獲取當(dāng)前的年月日
            String substring = format.substring(0, 11);
            stringBuilder.append(substring);

            //獲取日歷對(duì)象
            Calendar calendar = Calendar.getInstance();
            //獲取一天中當(dāng)前的小時(shí)數(shù) 24小時(shí)制的
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            //獲取秒殺場(chǎng)次
            if (hours % 2 == 0) {
                tv_screening.setText(hours + "點(diǎn)場(chǎng)");
                stringBuilder.append(hours + 2);
            } else {
                tv_screening.setText((hours - 1) + "點(diǎn)場(chǎng)");
                stringBuilder.append(hours + 1);
            }
            stringBuilder.append(":00:00");
            Date sessionDate = sdf.parse(stringBuilder.toString());
            //獲取秒殺場(chǎng)次+兩個(gè)小時(shí) 的時(shí)間戳
            long sessionDateTime = sessionDate.getTime();

            //獲取當(dāng)前時(shí)間的時(shí)間戳
            Date date = new Date();
            long millisecond = date.getTime();

            //間隔時(shí)間戳
            long timestampMillisecond = sessionDateTime - millisecond;

            //剩余小時(shí)數(shù)
            long hour = timestampMillisecond / (1000 * 60 * 60);
            //剩余分鐘數(shù)
            long minute = (timestampMillisecond - hour * (1000 * 60 * 60)) / (1000 * 60);
            //第①種方法: 獲得剩余秒數(shù)
//            long second = (timestampMillisecond - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;

            //第②種方法: 獲得剩余秒數(shù)
            //取余數(shù) 得到的也是毫秒數(shù)
            long test = timestampMillisecond % (60 * 1000);
            //剩余的秒數(shù) Math.round按照四舍五入返回最接近參數(shù)的int型整數(shù)
            long second = Math.round((float) (test / 1000));

            tv_hours.setText("0" + hour);

            if (minute >= 10) {
                tv_minutes.setText(minute + "");
            } else {
                tv_minutes.setText("0" + minute);
            }

            if (second >= 10) {
                tv_second.setText(second + "");
            } else {
                tv_second.setText("0" + second);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上就是京東秒殺的具體實(shí)現(xiàn)

總結(jié)

到此這篇關(guān)于用Android實(shí)現(xiàn)京東秒殺功能詳解的文章就介紹到這了,更多相關(guān)Android京東秒殺功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實(shí)現(xiàn)蒙版彈出框效果

    Android實(shí)現(xiàn)蒙版彈出框效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)蒙版彈出框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能

    Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能

    Android存儲(chǔ)方式有很多種,在這里所用的存儲(chǔ)方式是SharedPreferrences, 其采用了Map數(shù)據(jù)結(jié)構(gòu)來存儲(chǔ)數(shù)據(jù),以鍵值的方式存儲(chǔ),可以簡(jiǎn)單的讀取與寫入,下面通過實(shí)例代碼給大家講解下,需要的朋友參考下吧
    2017-04-04
  • Android中巧妙的實(shí)現(xiàn)緩存詳解

    Android中巧妙的實(shí)現(xiàn)緩存詳解

    采用緩存,可以進(jìn)一步大大緩解數(shù)據(jù)交互的壓力,有的時(shí)候?yàn)榱丝焖俨樵儠?huì)被多次調(diào)用的數(shù)據(jù),或者構(gòu)建比較廢時(shí)的實(shí)例,我們一般使用緩存的方法。無論大型或小型應(yīng)用,靈活的緩存可以說不僅大大減輕了服務(wù)器的壓力,而且因?yàn)楦焖俚挠脩趔w驗(yàn)而方便了用戶。下面來一起看看吧。
    2016-11-11
  • Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn)

    Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn)

    這篇文章主要介紹了Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Android如何利用svg實(shí)現(xiàn)可縮放的地圖控件

    Android如何利用svg實(shí)現(xiàn)可縮放的地圖控件

    這篇文章主要給大家介紹了關(guān)于Android如何利用svg實(shí)現(xiàn)可縮放的地圖控件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Android實(shí)現(xiàn)Ant Design 自定義表單組件

    Android實(shí)現(xiàn)Ant Design 自定義表單組件

    Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧
    2017-06-06
  • Android實(shí)現(xiàn)通話自動(dòng)錄音

    Android實(shí)現(xiàn)通話自動(dòng)錄音

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)通話自動(dòng)錄音,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄

    flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android幀動(dòng)畫、補(bǔ)間動(dòng)畫、屬性動(dòng)畫用法詳解

    Android幀動(dòng)畫、補(bǔ)間動(dòng)畫、屬性動(dòng)畫用法詳解

    安卓的三種動(dòng)畫,幀動(dòng)畫,補(bǔ)間動(dòng)畫,屬性動(dòng)畫,大家了解多少,知道如何使用嗎?本文就為大家簡(jiǎn)單介紹Android幀動(dòng)畫、補(bǔ)間動(dòng)畫、屬性動(dòng)畫的使用方法,需要的朋友可以參考下
    2016-11-11

最新評(píng)論