欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)訂單倒計(jì)時(shí)功能

 更新時(shí)間:2017年01月23日 14:16:10   作者:哇卡卡666  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)訂單倒計(jì)時(shí)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先上效果圖

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="yascn.com.timecalc.MainActivity">



 <TextView
  android:textSize="20dp"
  android:layout_centerInParent="true"
  android:id="@+id/tv_remaintime"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="#000000"
  />
</RelativeLayout>


2.MainActivity.class

package yascn.com.timecalc;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {


 TextView tv_remaintime;//倒計(jì)時(shí)
 private long countdownTime;//倒計(jì)時(shí)的總時(shí)間(單位:毫秒)
 private String timefromServer;//從服務(wù)器獲取的訂單生成時(shí)間
 private long chaoshitime;//從服務(wù)器獲取訂單有效時(shí)長(單位:毫秒)

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tv_remaintime = (TextView) findViewById(R.id.tv_remaintime);

  getTimeDuring();

 }


 Handler handler = new Handler();
 Runnable runnable = new Runnable() {
  @Override
  public void run() {
   countdownTime -= 1000;//倒計(jì)時(shí)總時(shí)間減1

   SimpleDateFormat minforamt = new SimpleDateFormat("mm:ss");

   String hms = minforamt.format(countdownTime);//格式化倒計(jì)時(shí)的總時(shí)間
   tv_remaintime.setText("還剩下" + hms);
   handler.postDelayed(this, 1000);
  }
 };


 private void getTimeDuring() {
  chaoshitime = 30 * 60 * 1000;//應(yīng)該從服務(wù)器獲取

  timefromServer = "2017-01-23 11:40:50";//應(yīng)該從服務(wù)器獲取
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  try {
   Date serverDate = df.parse(timefromServer);

   long duringTime = new Date().getTime() - serverDate.getTime();//計(jì)算當(dāng)前時(shí)間和從服務(wù)器獲取的訂單生成時(shí)間的時(shí)間差
   countdownTime = chaoshitime - duringTime;//計(jì)算倒計(jì)時(shí)的總時(shí)間

   handler.postDelayed(runnable, 1000);

  } catch (ParseException e) {
   e.printStackTrace();
  }


 }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論