嘿嘿嘿~~~,自己咕噥的.留著備用,若有bug,歡迎指出來呀
上圖:
演示圖.gif
以下自定義的textView的代碼,當然了Button也可以實現,看你自己發揮了,可創造性太高啦
TimeRunTextView.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by mcy
*/
@SuppressLint("AppCompatCustomView")
public class TimeRunTextView extends TextView {
private long mHour;//小時
private long mMin;//分鐘
private long mSecond;//秒
private OnTimeViewListener timeViewListener;//時間結束
private String MODE = "1";//時間展示模式
private Timer timer = null;
private TimerTask timerTask = null;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String getResult = (String) msg.obj;
TimeRunTextView.this.setText(getResult);
}
};
public TimeRunTextView(Context context) {
super(context);
}
public TimeRunTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TimeRunTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setTimeViewListener(OnTimeViewListener timeViewListener) {
this.timeViewListener = timeViewListener;
}
public void startTime(long timeCount, String mode) {
initTime();//先清空時間的工具類
if (!TextUtils.isEmpty(mode)) {
this.MODE = mode;
}
startTime(timeCount);
}
public void startTime(long timeCount) {
initTime();//先清空時間的工具類
mHour = timeCount / (60 * 60); // 對3600 取整 就是小時
mMin = (timeCount % (60 * 60)) / 60;// 對3600 取余除以60 就是多出的分
mSecond = timeCount % 60; // 對60 取余 就是多出的秒
if (timer == null) {
timer = new Timer();
}
if (timerTask == null) {
timerTask = new TimerTask() {
@Override
public void run() {
if ((mHour + mMin + mSecond) > 0) {
ComputeTime();//計算時分秒
} else {
stopTime();
}
}
};
}
timer.schedule(timerTask, 0, 1000);
if (timeViewListener != null) {
timeViewListener.onTimeStart();
}
}
public void stopTime() {
try {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
} catch (Exception e) {
}
if (timeViewListener != null) {
timeViewListener.onTimeEnd();
}
}
public void initTime() {
try {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
} catch (Exception e) {
}
}
private void ComputeTime() {
if (mSecond == 0) {
// 秒為0 判斷min 是否為0
if (mMin == 0) {
// 分為0 判斷hour
if (mHour == 0) {
Log.e("mcy--", "時間結束"); // 秒 分 時 都為0 倒計時結束
} else {
// 此處為hour 不為0 秒 分 為0 所以 hour-- 秒 分 為 59
mHour--;
mMin = 59;
mSecond = 59;
}
} else {
// 分不為0 所以不用修改 hour 分減一即可 秒 變為 59
mMin--;
mSecond = 59;
}
} else {
// 秒不為0 秒減一 分 和時 不變 繼續循環
mSecond--;
}
String strTime = "";
switch (MODE) {
case "1":
strTime = mHour + "時" + mMin + "分" + mSecond + "秒";
break;
case "2":
strTime = mHour + ":" + mMin + ":" + mSecond;
break;
default:
strTime = mHour + "時" + mMin + "分" + mSecond + "秒";
break;
}
Message msg = Message.obtain();
msg.obj = strTime; //從這里把你想傳遞的數據放進去就行了
handler.sendMessage(msg);
}
public interface OnTimeViewListener {
void onTimeStart();
void onTimeEnd();
}
}
以下是activity_main.xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mcy.test.activities.third.HuaLangActivity">
<include layout="@layout/base_top_layout" />
<TimeRunTextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:gravity="center"
android:text="00:00:00"
android:textSize="30sp" />
<EditText
android:id="@+id/rdtime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/text_20"
android:hint="輸入時間" />
<EditText
android:id="@+id/rdmode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/text_20"
android:hint="輸入模式" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 -->00時00分00秒\n2 -->00:00:00\n默認:時分秒"
android:textSize="@dimen/sp_16" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:text="開始" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:text="停止" />
</LinearLayout>
以下是MainActivity代碼:
public class MainActivity extends BaseActivity {
@BindView(R.id.rdtime)
EditText rdtime;
@BindView(R.id.start)
Button start;
@BindView(R.id.stop)
Button stop;
@BindView(R.id.time)
TimeRunTextView time;
@BindView(R.id.rdmode)
EditText rdmode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setTitle("倒計時");
time.setTimeViewListener(new TimeRunTextView.OnTimeViewListener() {
@Override
public void onTimeStart() {
Log.e("mcy" + "開始倒計時啦");
}
@Override
public void onTimeEnd() {
Log.e("mcy" + "時間停止了");
}
});
}
@OnClick({R.id.start, R.id.stop})
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
if (!TextUtils.isEmpty(Long.parseLong(rdtime.getText().toString().trim())) {
time.startTime(Long.parseLong(rdtime.getText().toString().trim()), Integer.parseInt(rdmode.getText().toString().trim()));
}else{
Log.e("mcy" + "請輸入時間哦");
}
break;
case R.id.stop:
time.stopTime();
break;
}
}
@Override
protected void onDestroy() {
//防止內存泄露
time.stopTime();
super.onDestroy();
}
}