直播電商原始碼,商品出售倒數計時的定時器效果

zhibo系統開發發表於2021-11-05

直播電商原始碼,商品出售倒數計時的定時器效果實現的相關程式碼

在MainActivity中,設定螢幕為橫屏

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


編寫佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
    xmlns:tools="
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/countdown_layout"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="大學生活還剩:"
        android:textColor="@color/design_default_color_error"
        android:textSize="30sp"
        android:textStyle="bold"
        android:gravity="center"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal">
        <TextView
            style="@style/count_down_time"
            android:background="@color/cardview_shadow_start_color"
            android:id="@+id/week"
            android:text="11月"
            />
        <TextView
            style="@style/count_down_time"
            android:background="@color/cardview_shadow_start_color"
            android:id="@+id/day"
            android:text="00天"
            />
        <TextView
            style="@style/count_down_time"
            android:background="@color/cardview_shadow_start_color"
            android:id="@+id/hour"
            android:text="01時"
            />
        <TextView
            style="@style/count_down_time"
            android:background="@color/cardview_shadow_start_color"
            android:id="@+id/minute"
            android:text="07分"
            />
        <TextView
            style="@style/count_down_time"
            android:background="@color/cardview_shadow_start_color"
            android:id="@+id/second"
            android:text="07秒"
            />
    </LinearLayout>
</LinearLayout>



找到控制元件,可以新增jake大佬的butterknife依賴,在app中的build.gradle依賴中新增兩行

implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'


實現思路將未來的某個時間,減去當前時間,並格式化成日期,賦值給TextView,運用定時器迴圈執行,MainActivity的全部程式碼。

package com.example.schoolcountdown;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.TextView;
import com.blankj.utilcode.util.LogUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
    //繫結對應的控制元件
    @BindView(R.id.week)
    TextView mWeek;
    @BindView(R.id.day)
    TextView mDay;
    @BindView(R.id.hour)
    TextView mHour;
    @BindView(R.id.minute)
    TextView mMinute;
    @BindView(R.id.second)
    TextView mSecond;
    private long targetTime;
    private Calendar calendar;
    private long currentTime;
    private long timeLeft;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE設定為橫屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //繫結黃油刀的依賴
        ButterKnife.bind(this);
        //============================================
        //獲取目標時間
        //測試定時器
        timerRun(0,1);
    }
    private void getTargetTime() {
        //java.util.Calendar
        calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(2022,5,24,12,0,0);
        targetTime = calendar.getTimeInMillis();
    }
    private void timerRun(long delay,long period) {
        Runnable runable = new Runnable(){
            public void run() {
                setAllText();
            }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runable, delay, period, TimeUnit.SECONDS);
    }
    private void setAllText() {
        getTargetTime();
        //yyyy-MM-dd hh:mm:ss
        currentTime = System.currentTimeMillis();
        timeLeft = targetTime - currentTime;
        Date date = new Date(timeLeft);
        /*LogUtils.d(targetTime + "------->" + currentTime + "=======" + timeLeft);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        LogUtils.d("時間為" + format);*/
        DateFormat formatter1 = new SimpleDateFormat("MM");
        setTime(formatter1,date);
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        setTime1(dd,date);
        SimpleDateFormat hh = new SimpleDateFormat("hh");
        setTime2(hh,date);
        SimpleDateFormat mm = new SimpleDateFormat("mm");
        setTime3(mm,date);
        SimpleDateFormat ss = new SimpleDateFormat("ss");
        setTime4(ss,date);
    }
    private void setTime4(SimpleDateFormat ss, Date date) {
        String format = ss.format(date);
        mSecond.setText(format + "秒");
    }
    private void setTime3(SimpleDateFormat mm, Date date) {
        String format = mm.format(date);
        mMinute.setText(format + "分鐘");
    }
    private void setTime2(SimpleDateFormat hh, Date date) {
        String format = hh.format(date);
        mHour.setText(format + "小時");
    }
    private void setTime1(SimpleDateFormat dd, Date date) {
        String format = dd.format(date);
        mDay.setText(format + "天");
    }
    private void setTime(DateFormat formatter1, Date date) {
        String format = formatter1.format(date);
        mWeek.setText(format + "月 ");
    }
}


以上就是 直播電商原始碼,商品出售倒數計時的定時器效果實現的相關程式碼,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2840795/,如需轉載,請註明出處,否則將追究法律責任。

相關文章