最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)訂單倒計時功能

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

先上效果圖

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;//倒計時
 private long countdownTime;//倒計時的總時間(單位:毫秒)
 private String timefromServer;//從服務器獲取的訂單生成時間
 private long chaoshitime;//從服務器獲取訂單有效時長(單位:毫秒)

 @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;//倒計時總時間減1

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

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


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

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

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

   handler.postDelayed(runnable, 1000);

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


 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

广宗县| 祁阳县| 和政县| 凉山| 游戏| 恩施市| 南昌市| 溧阳市| 花莲市| 镇赉县| 庆阳市| 宜兰县| 新野县| 安宁市| 裕民县| 武城县| 卫辉市| 建平县| 平阳县| 永春县| 中西区| 南川市| 长汀县| 刚察县| 新兴县| 镶黄旗| 宣恩县| 奈曼旗| 南靖县| 枣阳市| 潞西市| 关岭| 沅江市| 金华市| 晋宁县| 高碑店市| 赤壁市| 区。| 乡城县| 襄樊市| 辽阳县|