本文實例為大家分享了Android實現(xiàn)時間倒計時功能展示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)公司服務(wù)項目包括新安網(wǎng)站建設(shè)、新安網(wǎng)站制作、新安網(wǎng)頁制作以及新安網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,新安網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到新安省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
效果展示
MainActivity(主頁面代碼)
public class MainActivity extends Activity { private RelativeLayout countDown; // 倒計時 private TextView daysTv, hoursTv, minutesTv, secondsTv; private long mDay = 10; private long mHour = 10; private long mMin = 30; private long mSecond = 00;// 天 ,小時,分鐘,秒 private boolean isRun = true; private Handler timeHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1) { computeTime(); daysTv.setText(mDay+""); hoursTv.setText(mHour+""); minutesTv.setText(mMin+""); secondsTv.setText(mSecond+""); if (mDay==0&&mHour==0&&mMin==0&&mSecond==0) { countDown.setVisibility(View.GONE); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countDown = (RelativeLayout) findViewById(R.id.countdown_layout); daysTv = (TextView) findViewById(R.id.days_tv); hoursTv = (TextView) findViewById(R.id.hours_tv); minutesTv = (TextView) findViewById(R.id.minutes_tv); secondsTv = (TextView) findViewById(R.id.seconds_tv); 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--; if (mHour < 0) { // 倒計時結(jié)束 mHour = 23; mDay--; } } } } }
main(主頁面布局)
<?xml version="1.0" encoding="utf-8"?>
謝謝觀看,小編祝大家生活愉快!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。