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

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

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

首先看效果圖:

在這里插入圖片描述

京東秒殺是兩個(gè)小時(shí)一個(gè)場次,我們獲取到場次后,通過場次+兩個(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)場:"
            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 {
    //秒殺場次
    private TextView tv_screening;
    //2個(gè)小時(shí)一個(gè)秒殺場次,距離秒殺結(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ì)算秒殺場次,兩個(gè)小時(shí)一個(gè)場次
        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);

            //獲取日歷對象
            Calendar calendar = Calendar.getInstance();
            //獲取一天中當(dāng)前的小時(shí)數(shù) 24小時(shí)制的
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            //獲取秒殺場次
            if (hours % 2 == 0) {
                tv_screening.setText(hours + "點(diǎn)場");
                stringBuilder.append(hours + 2);
            } else {
                tv_screening.setText((hours - 1) + "點(diǎn)場");
                stringBuilder.append(hours + 1);
            }
            stringBuilder.append(":00:00");
            Date sessionDate = sdf.parse(stringBuilder.toString());
            //獲取秒殺場次+兩個(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android 自定義View步驟

    Android 自定義View步驟

    這篇文章主要介紹了Android 自定義View步驟 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android中使用Post請求的方法

    Android中使用Post請求的方法

    這篇文章主要介紹了Android中使用Post請求的方法,實(shí)例分析了Android中使用post請求的原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • android使用flutter的ListView實(shí)現(xiàn)滾動(dòng)列表的示例代碼

    android使用flutter的ListView實(shí)現(xiàn)滾動(dòng)列表的示例代碼

    現(xiàn)如今打開一個(gè) App,比如頭條、微博,都會(huì)有長列表,那么android使用flutter的ListView滾動(dòng)列表如何實(shí)現(xiàn),本文就來詳細(xì)的介紹一下,感興趣的同學(xué)可以來了解一下
    2018-12-12
  • Android深入分析屬性動(dòng)畫源碼

    Android深入分析屬性動(dòng)畫源碼

    這篇文章主要給大家介紹了關(guān)于Android動(dòng)畫系列教程之屬性動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Flutter 仿微信支付界面

    Flutter 仿微信支付界面

    網(wǎng)傳微信支付頁面的第三方鏈接一個(gè)格子需要廣告費(fèi)1一個(gè)億,微信支付頁非常適合做功能導(dǎo)航,本篇使用 ListView和 GridView 模仿了微信支付的頁面,同時(shí)介紹了如何裝飾一個(gè)組件的背景和邊緣樣式。
    2021-05-05
  • Android編程開發(fā)之RadioGroup用法實(shí)例

    Android編程開發(fā)之RadioGroup用法實(shí)例

    這篇文章主要介紹了Android編程開發(fā)之RadioGroup用法,結(jié)合實(shí)例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下
    2015-12-12
  • Android自定義View仿QQ健康界面

    Android自定義View仿QQ健康界面

    這篇文章主要為大家詳細(xì)介紹了Android自定義View仿QQ健康界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù)

    Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù)

    這篇文章主要介紹了Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)

    ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)

    之前寫的綁定數(shù)據(jù)是只是簡單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)
    2013-06-06
  • android中AutoCompleteTextView的簡單用法(實(shí)現(xiàn)搜索歷史)

    android中AutoCompleteTextView的簡單用法(實(shí)現(xiàn)搜索歷史)

    本篇文章主要介紹了android中AutoCompleteTextView的簡單用法(自動(dòng)提示),有需要的可以了解一下。
    2016-11-11

最新評論

广宗县| 绥棱县| 揭西县| 嘉善县| 宣威市| 虞城县| 舞阳县| 河南省| 聂拉木县| 台北市| 南部县| 东乡县| 三穗县| 阿坝| 宁明县| 马公市| 双桥区| 彩票| 阳原县| 宁晋县| 乌拉特中旗| 蒲城县| 佛学| 黑山县| 且末县| 绥江县| 鹤山市| 三原县| 嵊泗县| 盐亭县| 固镇县| 奈曼旗| 宜宾市| 获嘉县| 揭阳市| 章丘市| 台北市| 美姑县| 永新县| 揭阳市| 荃湾区|