这篇文章给大家分享的是有关Andorid如何实现精确到天时分秒的抢购倒计时的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
示例:
package com.qust.widght; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.qust.rushbuycountdowntimerview.R; @SuppressLint("HandlerLeak") public class RushBuyCountDownTimerView extends LinearLayout { // 天,十位 private TextView tv_day_decade; // 天,个位 private TextView tv_day_unit; // 小时,十位 private TextView tv_hour_decade; // 小时,个位 private TextView tv_hour_unit; // 分钟,十位 private TextView tv_min_decade; // 分钟,个位 private TextView tv_min_unit; // 秒,十位 private TextView tv_sec_decade; // 秒,个位 private TextView tv_sec_unit; private Context context; private int day_decade; private int day_unit; private int hour_decade; private int hour_unit; private int min_decade; private int min_unit; private int sec_decade; private int sec_unit; // 计时器 private Timer timer; private Handler handler = new Handler() { public void handleMessage(Message msg) { countDown(); }; }; private int day = 0; private int hour = 0; private int min = 0; private int sec = 0; public RushBuyCountDownTimerView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.view_countdowntimer, this); tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade); tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit); tv_day_decade = (TextView) view.findViewById(R.id.tv_day_decade); tv_day_unit = (TextView) view.findViewById(R.id.tv_day_unit); tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade); tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit); tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade); tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit); } /** * * @Description: 开始计时 * @param * @return void * @throws */ public void start() { if (timer == null) { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0); } }, 0, 1000); } } /** * * @Description: 停止计时 * @param * @return void * @throws */ public void stop() { if (timer != null) { timer.cancel(); timer = null; } } // 如果:sum = 12345678 public void addTime(int sum) { // 求出天数 int day = sum / 60 / 60 / 24; // int day_time = sum % 24; Log.e("小时", day + ""); Log.e("小时", sum % 24 + ""); // 求出小时 // int hour = day_time / 60; // int hour_time = day_time % 60; // // Log.e("小时", hour + ""); // // 先获取个秒数值 int sec = sum % 60; // 如果大于60秒,获取分钟。(秒数) int sec_time = sum / 60; // 再获取分钟 int min = sec_time % 60; // 如果大于60分钟,获取小时(分钟数)。 int min_time = sec_time / 60; // 获取小时 int hour = min_time % 24; // 剩下的自然是天数 day = min_time / 24; // // Log.e("分钟", min + ""); // // // 求出秒数 // Log.e("秒数", sec + ""); setTime(day, hour, min, sec); } /** * @throws Exception * * @Description: 设置倒计时的时长 * @param * @return void * @throws */ public void setTime(int day, int hour, int min, int sec) { //这里的天数不写也行,我写365 if (day >= 365 || hour >= 24 || min >= 60 || sec >= 60 || day < 0 || hour < 0 || min < 0 || sec < 0) { throw new RuntimeException( "Time format is error,please check out your code"); } // day 的十位数 day_decade = day / 10; // day的个位数,这里求余就行 day_unit = day - day_decade * 10; hour_decade = hour / 10; hour_unit = hour - hour_decade * 10; min_decade = min / 10; min_unit = min - min_decade * 10; sec_decade = sec / 10; sec_unit = sec - sec_decade * 10; // 第个time 进行初始化 timeClean(); } private void timeClean() { tv_day_decade.setText(day_decade + ""); tv_day_unit.setText(day_unit + ""); tv_hour_decade.setText(hour_decade + ""); tv_hour_unit.setText(hour_unit + ""); tv_min_decade.setText(min_decade + ""); tv_min_unit.setText(min_unit + ""); tv_sec_decade.setText(sec_decade + ""); tv_sec_unit.setText(sec_unit + ""); } /** * * @Description: 倒计时 * @param * @return boolean * @throws */ public Boolean countDown() { if (isCarry4Unit(tv_sec_unit)) { if (isCarry4Decade(tv_sec_decade)) { if (isCarry4Unit(tv_min_unit)) { if (isCarry4Decade(tv_min_decade)) { if (isDay4Unit(tv_hour_unit)) { if (isDay4Decade(tv_hour_decade)) { if (isDay4Unit(tv_day_unit)) { if (isDay4Decade(tv_day_decade)) { Toast.makeText(context, "时间到了", Toast.LENGTH_SHORT).show(); tv_day_decade.setText("0"); tv_day_unit.setText("0"); tv_hour_decade.setText("0"); tv_hour_unit.setText("0"); tv_min_decade.setText("0"); tv_min_unit.setText("0"); tv_sec_decade.setText("0"); tv_sec_unit.setText("0"); stop(); return false; } } } } } } } } return false; } /** * 进行——时分秒,判断个位数 * * @Description: 变化十位,并判断是否需要进位 * @param * @return boolean * @throws */ private boolean isCarry4Decade(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 5; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } /** * 进行——时分秒,判断个位数 * * @Description: 变化个位,并判断是否需要进位 * @param * @return boolean * @throws */ private boolean isCarry4Unit(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 9; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } /** * 进行——时分秒,判断个位数 * * @Description: 变化十位,并判断是否需要进位 * @param * @return boolean * @throws */ private boolean isDay4Unit(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 3; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } /** * 进行——时分秒,判断个位数 * * @Description: 变化个位,并判断是否需要进位 * @param * @return boolean * @throws */ private boolean isDay4Decade(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 2; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } }
主类进行调用
package com.qust.widght; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.qust.rushbuycountdowntimerview.R; public class MainActivity extends Activity { private RushBuyCountDownTimerView timerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerView = (RushBuyCountDownTimerView) findViewById(R.id.timerView); // 设置时间(day,hour,min,sec) // timerView.setTime(0, 0, 0, 5); int sum = 60; // 把秒数传到倒计时方法中。。 timerView.addTime(sum); // 开始倒计时 timerView.start(); } }
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.qust.widght.RushBuyCountDownTimerView android:id="@+id/timerView" android:layout_width="wrap_content" android:layout_height="wrap_content" > </com.qust.widght.RushBuyCountDownTimerView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal" android:padding="10dp" > <TextView android:id="@+id/tv_day_decade" /> <TextView android:id="@+id/tv_day_unit" android:layout_marginLeft="1dp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" android:text=":" android:textColor="#4F4242" android:textSize="30sp" /> <TextView android:id="@+id/tv_hour_decade" /> <TextView android:id="@+id/tv_hour_unit" android:layout_marginLeft="1dp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" android:text=":" android:textColor="#4F4242" android:textSize="30sp" /> <TextView android:id="@+id/tv_min_decade" /> <TextView android:id="@+id/tv_min_unit" android:layout_marginLeft="1dp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" android:text=":" android:textColor="#4F4242" android:textSize="30sp" /> <TextView android:id="@+id/tv_sec_decade" /> <TextView android:id="@+id/tv_sec_unit" android:layout_marginLeft="1dp" /> </LinearLayout>
感谢各位的阅读!关于“Andorid如何实现精确到天时分秒的抢购倒计时”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。