Android實現(xiàn)訂單倒計時功能
更新時間:2017年01月23日 14:16:10 作者:哇卡卡666
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)訂單倒計時的相關(guā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;//從服務(wù)器獲取的訂單生成時間
private long chaoshitime;//從服務(wù)器獲取訂單有效時長(單位:毫秒)
@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;//應(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();//計算當(dāng)前時間和從服務(wù)器獲取的訂單生成時間的時間差
countdownTime = chaoshitime - duringTime;//計算倒計時的總時間
handler.postDelayed(runnable, 1000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- android自定義倒計時控件示例
- android實現(xiàn)倒計時功能代碼
- Android實現(xiàn)加載廣告圖片和倒計時的開屏布局
- Android實現(xiàn)計時與倒計時的常用方法小結(jié)
- Android 實現(xiàn)閃屏頁和右上角的倒計時跳轉(zhuǎn)實例代碼
- Android開發(fā)之獲取短信驗證碼后按鈕背景變化并且出現(xiàn)倒計時
- Android賬號注冊實現(xiàn)點擊獲取驗證碼倒計時效果
- Android中CountDownTimer倒計時器用法實例
- Android自定義照相機倒計時拍照
- Android中使用TextView實現(xiàn)高仿京東淘寶各種倒計時效果
相關(guān)文章
Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實例
下面小編就為大家分享一篇Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android動畫之逐幀動畫(Frame Animation)基礎(chǔ)學(xué)習(xí)
大家都知道逐幀動畫是一種常見的動畫形式,其原理是在“連續(xù)的關(guān)鍵幀”中分解動畫動作,也就是在時間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動畫。下面我們就來學(xué)習(xí)下Android中逐幀動畫的基礎(chǔ)知識,有需要的可以參考借鑒。2016-09-09
Android實現(xiàn)五子棋游戲(局域網(wǎng)版)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)局域網(wǎng)版的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
AndroidStudio 配置 AspectJ 環(huán)境實現(xiàn)AOP的方法
本篇文章主要介紹了AndroidStudio 配置 AspectJ 環(huán)境實現(xiàn)AOP的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Android內(nèi)核源碼 在Ubuntu上下載,編譯,安裝
本文主要介紹Android內(nèi)核源碼,想學(xué)習(xí)深入研究Android的朋友肯定要看看Android內(nèi)核知識的,這里對下載Android內(nèi)核源代碼的下載,安裝,編譯做了詳細(xì)的介紹,有興趣的小伙伴可以參考下2016-08-08
Android自定義ScrollView實現(xiàn)放大回彈效果實例代碼
本篇文章主要介紹了Android自定義ScrollView實現(xiàn)放大回彈效果實例代碼,具有一定的參考價值,有興趣的可以了解一下。2017-03-03

