溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Android實現(xiàn)一個倒計時功能

發(fā)布時間:2020-11-20 16:40:16 來源:億速云 閱讀:159 作者:Leah 欄目:移動開發(fā)

這篇文章給大家介紹利用Android實現(xiàn)一個倒計時功能,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

先上效果圖

利用Android實現(xiàn)一個倒計時功能

1.activity_main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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();
  }


 }
}

關(guān)于利用Android實現(xiàn)一個倒計時功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI