直播电商源码,商品出售倒计时的定时器效果
直播电商源码,商品出售倒计时的定时器效果实现的相关代码
在MainActivity中,设置屏幕为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
编写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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 + "月 ");
}
}
以上就是 直播电商源码,商品出售倒计时的定时器效果实现的相关代码,更多内容欢迎关注之后的文章