Android 仿京東秒殺倒計(jì)時(shí)代碼
效果圖如下所示:
由于我仿的京東是分模塊的,所以,這次主要描述秒殺模塊!
首先設(shè)置好時(shí)間的背景
drawable文件下創(chuàng)建shape_miaosha_time.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#000"></solid> <corners android:radius="2.5dp"></corners> </shape>
然后主要布局,你可以單獨(dú)書寫,然后引用出去
**count_down.xml**
<?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="#FFFFFF"
android:orientation="vertical">
<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="8dp"
android:text="京東秒殺"
android:textColor="#f00"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_miaosha_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="12點(diǎn)場(chǎng)"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_miaosha_shi"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/shape_miaosha_time"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="15sp" />
<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="25dp"
android:layout_height="25dp"
android:background="@drawable/shape_miaosha_time"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="15sp" />
<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="25dp"
android:layout_height="25dp"
android:background="@drawable/shape_miaosha_time"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
這里寫邏輯代碼
//使用handler用于更新UI
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
countDown();
sendEmptyMessageDelayed(0, 1000);
}
};
/**
* 秒殺
*/
private void countDown() {
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) {
mMiaoshaTimeTv.setText(hour + "點(diǎn)場(chǎng)");
buffer.append((hour + 2));
buffer.append(":00:00");
} else {
mMiaoshaTimeTv.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);
mMiaoshaShiTv.setText("0" + hours + "");
if (minute >= 10) {
mMiaoshaMinterTv.setText(minute + "");
} else {
mMiaoshaMinterTv.setText("0" + minute + "");
}
if (second >= 10) {
mMiaoshaSecondTv.setText(second + "");
} else {
mMiaoshaSecondTv.setText("0" + second + "");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
注意,這里才是開啟的代碼
private void startCountDown() {
handler.sendEmptyMessage(0);
}
總結(jié)
以上所述是小編給大家介紹的Android 仿京東秒殺倒計(jì)時(shí)代碼 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android小程序?qū)崿F(xiàn)選項(xiàng)菜單
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)選項(xiàng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解
這篇文章主要為大家介紹了Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路
這篇文章主要為大家介紹了另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路,android5.0及以后版本都支持給狀態(tài)欄著色,而目前android主流版本還是4.4,想要深入了解的朋友可以參考一下2016-01-01
Android結(jié)合xml實(shí)現(xiàn)幀動(dòng)畫
將一組動(dòng)作相近的圖片組合在一起,然后按照一定的時(shí)間來播放,就會(huì)形成一個(gè)動(dòng)畫,我們可以稱之為幀動(dòng)畫。在 Android 中可通過結(jié)合 xml 的方式來輕松實(shí)現(xiàn)。2021-05-05
Android使用AlarmManager設(shè)置鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android使用AlarmManager設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
探討:如何修改Android超時(shí)休眠時(shí)間
本篇文章是對(duì)如何修改Android超時(shí)休眠時(shí)間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
這篇文章介紹了Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10
Android Studio項(xiàng)目中導(dǎo)入開源庫(kù)的方法
這篇文章主要介紹了Android Studio項(xiàng)目中導(dǎo)入開源庫(kù)的方法,即使用第三方庫(kù)、第三廣場(chǎng)框架的方法,需要的朋友可以參考下2015-06-06
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對(duì)大家有所幫助2016-02-02

