Android實(shí)現(xiàn)京東秒殺界面
本文實(shí)例為大家分享了Android實(shí)現(xiàn)京東秒殺界面展示的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
京東秒殺是兩個(gè)小時(shí)一個(gè)場(chǎng)次,判斷本機(jī)的時(shí)間進(jìn)行場(chǎng)次定時(shí),然后在這兩個(gè)小時(shí)里面進(jìn)行倒計(jì)時(shí)。
MainActivity
package com.bwie.com.myapplication; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private TextView miaosha_time; private TextView miaosha_shi; private TextView miaosha_minter; private TextView miaosha_second; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); setTime(); sendEmptyMessageDelayed(0, 1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); handler.sendEmptyMessage(0); } public void initView(){ miaosha_time = (TextView) findViewById(R.id.tv_miaosha_time); miaosha_shi = (TextView) findViewById(R.id.tv_miaosha_shi); miaosha_minter = (TextView) findViewById(R.id.tv_miaosha_minter); miaosha_second = (TextView) findViewById(R.id.tv_miaosha_second); } //秒殺倒計(jì)時(shí) public void setTime() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis()); String format = df.format(curDate); StringBuffer buffer = new StringBuffer(); String substring = format.substring(0, 11); buffer.append(substring); Log.d("ccc", substring); Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); if (hour % 2 == 0) { miaosha_time.setText(hour + "點(diǎn)場(chǎng)"); buffer.append((hour + 2)); buffer.append(":00:00"); } else { miaosha_time.setText((hour - 1) + "點(diǎn)場(chǎng)"); buffer.append((hour + 1)); buffer.append(":00:00"); } String totime = buffer.toString(); try { java.util.Date date = df.parse(totime); java.util.Date date1 = df.parse(format); long defferenttime = date.getTime() - date1.getTime(); long days = defferenttime / (1000 * 60 * 60 * 24); long hours = (defferenttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); long minute = (defferenttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60); long seconds = defferenttime % 60000; long second = Math.round((float) seconds / 1000); miaosha_shi.setText("0" + hours + ""); if (minute >= 10) { miaosha_minter.setText(minute + ""); } else { miaosha_minter.setText("0" + minute + ""); } if (second >= 10) { miaosha_second.setText(second + ""); } else { miaosha_second.setText("0" + second + ""); } } catch (ParseException e) { e.printStackTrace(); } } }
布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.bwie.com.myapplication.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical"> <TextView android:id="@+id/tv_miaosha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="京東秒殺" android:textColor="#f00" /> <TextView android:id="@+id/tv_miaosha_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="10點(diǎn)場(chǎng)" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_miaosha_shi" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/shape_miaosha_time" android:gravity="center" android:text="1" android:textColor="#fff" android:textSize="10sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:text=":" /> <TextView android:id="@+id/tv_miaosha_minter" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/shape_miaosha_time" android:gravity="center" android:text="1" android:textColor="#fff" android:textSize="10sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:text=":" /> <TextView android:id="@+id/tv_miaosha_second" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/shape_miaosha_time" android:gravity="center" android:text="1" android:textColor="#fff" android:textSize="10sp" /> </LinearLayout> </LinearLayout> </RelativeLayout>
shape_miaosha_time.xml(對(duì)倒計(jì)時(shí)小黑框圓角的實(shí)現(xiàn))
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#000"></solid> <corners android:radius="3dp"></corners> </shape>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ConstraintLayout約束布局使用詳解
ConstraintLayout 即約束布局,也是 Android Studio 的默認(rèn)布局,它可以減少布局的層級(jí),改善布局性能。不夸張地說(shuō),它基本上可以實(shí)現(xiàn)任何你想要的布局效果,下面,咱們一起來(lái)瞧瞧吧2022-11-11Android中判斷l(xiāng)istview是否滑動(dòng)到頂部和底部的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android中判斷l(xiāng)istview是否滑動(dòng)到頂部和底部的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android設(shè)置Padding和Margin(動(dòng)態(tài)/靜態(tài))的方法實(shí)例
如何在java代碼中設(shè)置margin,也就是組件與組件之間的間距,下面這篇文章主要給大家介紹了關(guān)于Android設(shè)置Padding和Margin(動(dòng)態(tài)/靜態(tài))的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Android 修改Preferences默認(rèn)樣式的步驟
這篇文章主要介紹了Android 修改Preferences默認(rèn)樣式的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04ViewPager實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了ViewPager實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android編程實(shí)現(xiàn)改變控件背景及形態(tài)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)改變控件背景及形態(tài)的方法,涉及Android控件布局設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android自定義Gallery控件實(shí)現(xiàn)3D圖片瀏覽器
這篇文章主要介紹了Android自定義Gallery控件實(shí)現(xiàn)3D圖片瀏覽器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04修改Android簽名證書(shū)keystore的密碼、別名alias以及別名密碼
這篇文章主要介紹了修改Android簽名證書(shū)keystore的密碼、別名alias以及別名密碼的相關(guān)資料,需要的朋友可以參考下2015-12-12android RecyclerView的一些優(yōu)化點(diǎn)介紹
大家好,本篇文章主要講的是android RecyclerView的一些優(yōu)化點(diǎn)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12利用kotlin實(shí)現(xiàn)一個(gè)餅圖實(shí)例代碼
餅狀圖是以不同顏色的圓的切片表示的值。下面這篇文章主要給大家介紹了關(guān)于利用kotlin實(shí)現(xiàn)一個(gè)餅圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12