1、自定义Textview类
package utils;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class TimerTextView extends TextView implements Runnable {
public static String Ltime = null;
public TimerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
private long mmin, msecond;
public boolean run = false;
public void setTimes(long[] times) {
mmin = times[0];
msecond = times[1];
}
private void ComputeTime() {
msecond--;
if (msecond < 0) {
mmin--;
msecond = 59;
}
}
public boolean isRun() {
return run;
}
//开始倒计时
public void beginRun() {
this.run = true;
run();
}
//暂停操作
public void stopRun() {
this.run = false;
this.removeCallbacks(this);//暂停时移除当前线程,否则会加快运行
}
//多线程,时间按mm:ss格式显示
@Override
public void run() {
if (this.run) {
ComputeTime();
if (mmin > 9 && msecond > 9) {
String strTime = mmin + ":" + msecond;
this.setText(strTime);
} else if (mmin > 9 && msecond < 10) {
String strTime = mmin + ":0" + msecond;
this.setText(strTime);
} else if (mmin < 10 && msecond > 9) {
String strTime = "0" + mmin + ":" + msecond;
this.setText(strTime);
} else if (mmin < 10 && msecond < 10) {
String strTime = "0" + mmin + ":0" + msecond;
this.setText(strTime);
}
this.postDelayed(this, 1000);
}
if (mmin == 0 && msecond == 0) {
stopRun();
}
}
//剩余时间的分和秒
public long getRemainMinute() {
return mmin;
}
public long getRemainSecond(){
return msecond;
}
}
2、MainActivity
package com.harvic.trytimerview;
/**
* @author harvic
* @address blog.youkuaiyun.com/harvic880925
* @date 2014-12-17
*/
import java.util.Calendar;
import com.example.trytimerview.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化倒计时控件
final TimerTextView timerTextView = (TimerTextView)findViewById(R.id.timer_text_view);
long[] times = {0,10,5,30};
timerTextView.setTimes(times);
Button startBtn = (Button)findViewById(R.id.main_start_btn);
Button stopBtn = (Button)findViewById(R.id.main_stop_btn);
//开始倒计时
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!timerTextView.isRun()){
timerTextView.beginRun();
}
}
});
//停止倒计时
stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(timerTextView.isRun()){
timerTextView.stopRun();
}
}
});
}
}
3、布局文件
<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"
tools:context="com.harvic.trytimerview.MainActivity" >
<Button android:id="@+id/main_start_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start run"/>
<Button android:id="@+id/main_stop_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop run"/>
<com.harvic.trytimerview.TimerTextView
android:id="@+id/timer_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff0000"
android:gravity="center_horizontal"
android:text="倒计时"
/>
</LinearLayout>