大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍Android如何实现京东秒杀界面,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括成都网站设计、成都网站制作、电商网站开发、微信营销、系统平台开发。具体内容如下
效果图:
京东秒杀是两个小时一个场次,判断本机的时间进行场次定时,然后在这两个小时里面进行倒计时。
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); } //秒杀倒计时 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 + "点场"); buffer.append((hour + 2)); buffer.append(":00:00"); } else { miaosha_time.setText((hour - 1) + "点场"); 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
shape_miaosha_time.xml(对倒计时小黑框圆角的实现)
以上是“Android如何实现京东秒杀界面”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!