Android限时抢购倒计时实现代码
程序员文章站
2023-12-17 22:36:40
限时抢购倒计时实现效果图
布局:
限时抢购倒计时实现效果图
布局:
<linearlayout android:id="@+id/ll_xsqg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingtop="8dp" android:paddingbottom="8dp" android:paddingleft="16dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textstyle="bold" android:textsize="14sp" android:text="@string/xsqg"/> <textview android:id="@+id/tv_hour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:paddingtop="3dp" android:paddingbottom="3dp" android:paddingleft="5dp" android:paddingright="5dp" android:background="@drawable/time_corner" android:textcolor="@android:color/white" android:textsize="12sp" android:text="02"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:textstyle="bold" android:textcolor="@android:color/black" android:text=":"/> <textview android:id="@+id/tv_minute" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:paddingtop="3dp" android:paddingbottom="3dp" android:paddingleft="5dp" android:paddingright="5dp" android:background="@drawable/time_corner" android:textcolor="@android:color/white" android:textsize="12sp" android:text="15"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:textstyle="bold" android:textcolor="@android:color/black" android:text=":"/> <textview android:id="@+id/tv_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:paddingtop="3dp" android:paddingbottom="3dp" android:paddingleft="5dp" android:paddingright="5dp" android:background="@drawable/time_corner" android:textcolor="@android:color/white" android:textsize="12sp" android:text="36"/> </linearlayout>
代码实现
public class homeactivity extends activity { @bind(r.id.tv_hour) textview tvhour; @bind(r.id.tv_minute) textview tvminute; @bind(r.id.tv_second) textview tvsecond; private long mhour = 02; private long mmin = 15; private long msecond = 36; private boolean isrun = true; private handler timehandler = new handler() { @override public void handlemessage(message msg) { super.handlemessage(msg); if (msg.what==1) { computetime(); if (mhour<10){ tvhour.settext("0"+mhour+""); }else { tvhour.settext("0"+mhour+""); } if (mmin<10){ tvminute.settext("0"+mmin+""); }else { tvminute.settext(mmin+""); } if (msecond<10){ tvsecond.settext("0"+msecond+""); }else { tvsecond.settext(msecond+""); } } } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_home); butterknife.bind(this); startrun(); } /** * 开启倒计时 */ private void startrun() { new thread(new runnable() { @override public void run() { // todo auto-generated method stub while (isrun) { try { thread.sleep(1000); // sleep 1000ms message message = message.obtain(); message.what = 1; timehandler.sendmessage(message); } catch (exception e) { e.printstacktrace(); } } } }).start(); } /** * 倒计时计算 */ private void computetime() { msecond--; if (msecond < 0) { mmin--; msecond = 59; if (mmin < 0) { mmin = 59; mhour--; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。