public class MainActivity extends AppCompatActivity implements Chronometer.OnChronometerTickListener, View.OnClickListener {
private Chronometer mChronometer;
int current = 0;
private MyCountDownTimer myCountDownTimer;
private TextView mTxt_count;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState) ;
setContentView( R.layout.activity_main) ;
mChronometer = findViewById( R.id.chronometer) ;
Button btn_start = findViewById( R.id.btn_start) ;
Button btn_close = findViewById( R.id.btn_close) ;
Button btn_count = findViewById( R.id.btn_count) ;
mTxt_count = findViewById( R.id.txt_count) ;
btn_start.setOnClickListener( this) ;
btn_close.setOnClickListener( this) ;
btn_count.setOnClickListener( this) ;
//倒计时初始化60秒
myCountDownTimer = new MyCountDownTimer( 60000, 1000) ;
//正数计时设置初始值(重置)
mChronometer.setBase( 0) ;
//正数计时事件监听器,时间发生变化时可进行操作
mChronometer.setOnChronometerTickListener( this) ;
//设置格式( 默认"MM:SS" 格式)
mChronometer.setFormat( "%s" ) ;
mChronometer.setText( FormatMiss( current)) ;
initData( ) ;
}
//倒计时函数
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer( long millisInFuture, long countDownInterval) {
super( millisInFuture, countDownInterval) ;
}
//计时过程
@Override
public void onTick( long l) {
//防止计时过程中重复点击
mTxt_count.setClickable( false) ;
mTxt_count.setText( l / 1000 + "秒" ) ;
}
//计时完毕的方法
@Override
public void onFinish( ) {
//重新给Button设置文字
mTxt_count.setText( "重新获取" ) ;
//设置可点击
mTxt_count.setClickable( true) ;
}
}
//正数计时
private void initData( ) {
mChronometer.setOnChronometerTickListener( new Chronometer.OnChronometerTickListener( ) {
@Override
public void onChronometerTick( Chronometer chronometer) {
current++;
chronometer.setText( FormatMiss( current)) ;
}
} ) ;
}
@Override
public void onChronometerTick( Chronometer chronometer) {
}
//正数计时显示格式
public static String FormatMiss( int time) {
String hh = time / 3600 > 9 ? time / 3600 + "" : "0" + time / 3600;
String mm = ( time % 3600) / 60 > 9 ? ( time % 3600) / 60 + "" : "0" + ( time % 3600) / 60;
String ss = ( time % 3600) % 60 > 9 ? ( time % 3600) % 60 + "" : "0" + ( time % 3600) % 60;
return hh + ":" + mm + ":" + ss;
}
@Override
public void onClick( View view) {
switch ( view.getId( )) {
case R.id.btn_start:
//开始
mChronometer.start( ) ;
break ;
case R.id.btn_close:
//结束
mChronometer.stop( ) ;
break ;
case R.id.btn_count:
//倒计时开始
myCountDownTimer.start( ) ;
break ;
}
}
}
< ?xml version= "1.0" encoding= "utf-8" ?>
< 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= ".MainActivity" >
< Chronometer
android:id= "@+id/chronometer"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center" />
< TextView
android:id= "@+id/txt_count"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center"
android:text= "60" />
< Button
android:id= "@+id/btn_start"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "开始" />
< Button
android:id= "@+id/btn_close"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "结束" />
< Button
android:id= "@+id/btn_count"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "倒计时" />
< /LinearLayout>