嘿嘿嘿~~~,自己咕哝的.留着备用,若有bug,欢迎指出来呀
上图:

以下自定义的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();
}
}
网友评论