Android實(shí)現(xiàn)訂單倒計(jì)時(shí)功能
先上效果圖

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í)長(zhǎng)(單位:毫秒)
@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();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android開發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android自定義照相機(jī)倒計(jì)時(shí)拍照
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
相關(guān)文章
Androd自定義對(duì)話框Dialog視圖及參數(shù)傳遞的實(shí)現(xiàn)方法
這篇文章主要介紹了Androd自定義對(duì)話框Dialog視圖及參數(shù)傳遞的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android使用GridView實(shí)現(xiàn)日歷的簡(jiǎn)單功能
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)日歷的簡(jiǎn)單功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(1)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
本人小菜一個(gè)。目前只見過兩種彈出框的實(shí)現(xiàn)方式,第一種是最常見的PopupWindow,第二種也就是Activity的方式是前幾天才見識(shí)過,需要的朋友可以參考下2012-11-11
Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android使用Room數(shù)據(jù)庫(kù)解決本地持久化的操作
Room 是一個(gè)持久性庫(kù),屬于 Android Jetpack 的一部分,Room 是 SQLite 數(shù)據(jù)庫(kù)之上的一個(gè)抽象層,Room 并不直接使用 SQLite,而是負(fù)責(zé)簡(jiǎn)化數(shù)據(jù)庫(kù)設(shè)置和配置以及與數(shù)據(jù)庫(kù)交互方面的瑣碎工作,本文介紹了Android使用Room數(shù)據(jù)庫(kù)解決本地持久化的操作,需要的朋友可以參考下2024-09-09

