這篇“怎么用Android實現京東秒殺功能”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么用Android實現京東秒殺功能”文章吧。
首先看效果圖:

京東秒殺是兩個小時一個場次,我們獲取到場次后,通過場次+兩個小時后,獲取到最終的時間,拿最終時間的時間戳,與當前時間時間戳相減,求得剩余的小時,分鐘,秒數,即可實現倒計時功能!
具體代碼實現如下:
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="幾點場:" 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類,具體注釋已經在代碼中給出
public class SeckillActivity extends AppCompatActivity {
//秒殺場次
private TextView tv_screening;
//2個小時一個秒殺場次,距離秒殺結束剩余多少小時
private TextView tv_hours;
//剩余分鐘數
private TextView tv_minutes;
//剩余秒數
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);
//計算秒殺場次,兩個小時一個場次
handler.sendEmptyMessage(0x00);
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what == 0x00) {
//設置時間
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());
//獲取當前的年月日
String substring = format.substring(0, 11);
stringBuilder.append(substring);
//獲取日歷對象
Calendar calendar = Calendar.getInstance();
//獲取一天中當前的小時數 24小時制的
int hours = calendar.get(Calendar.HOUR_OF_DAY);
//獲取秒殺場次
if (hours % 2 == 0) {
tv_screening.setText(hours + "點場");
stringBuilder.append(hours + 2);
} else {
tv_screening.setText((hours - 1) + "點場");
stringBuilder.append(hours + 1);
}
stringBuilder.append(":00:00");
Date sessionDate = sdf.parse(stringBuilder.toString());
//獲取秒殺場次+兩個小時 的時間戳
long sessionDateTime = sessionDate.getTime();
//獲取當前時間的時間戳
Date date = new Date();
long millisecond = date.getTime();
//間隔時間戳
long timestampMillisecond = sessionDateTime - millisecond;
//剩余小時數
long hour = timestampMillisecond / (1000 * 60 * 60);
//剩余分鐘數
long minute = (timestampMillisecond - hour * (1000 * 60 * 60)) / (1000 * 60);
//第①種方法: 獲得剩余秒數
// long second = (timestampMillisecond - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;
//第②種方法: 獲得剩余秒數
//取余數 得到的也是毫秒數
long test = timestampMillisecond % (60 * 1000);
//剩余的秒數 Math.round按照四舍五入返回最接近參數的int型整數
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();
}
}
}以上就是關于“怎么用Android實現京東秒殺功能”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。