Android计时器和倒计时的实现(含开始,暂停,和复位)
倒计时和计时在比赛中经常用到,由于涉及到子线程和主线程的通信问题,同学们在制作的时候总有点畏手畏脚的,今天会有两个神器出现,帮助大家攻克这两道难关!
神器一:chronometer --andrdoid 控件的一种,自带监听方法,使用前需要将该控件设置在xml配置文件里
神奇二:CountDownTimer---代码控制的倒计时,只需要调用textview显示即可
==================================================================
【计时】:
一切从简单开始,首先制作计时功能:
拖动该控件到graph页面上,xml文档如下:
<TextView android:id="@+id/ctdtxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:textSize="40dp" android:layout_gravity="center" android:text="" /> <Chronometer android:id="@+id/chronometer1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Chronometer" android:textSize="80dp" />
一个textview用来显示,下面就是带的计时器了,接下来就是代码大显身手的时候
主Activity
private class OnChronometer implements OnChronometerTickListener{ @Override public void onChronometerTick(Chronometer arg0) { // TODO Auto-generated method stub String time=clock.getText().toString().replaceAll("[^(\\d{2}:\\d{2})]", ""); if((hour+":"+minute).equals(time)){ GameStart.this.vibrator.vibrate(new long[]{1000, 10,1000,100},0);//设置震动周期以及循环震动 } } }
private class StartListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub GameStart.this.clock.setBase(SystemClock.elapsedRealtime()-recordTime);// GameStart.this.clock.start();//开始计时 } private class StopListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub GameStart.this.clock.stop();//停止计时 GameStart.this.vibrator.cancel();//取消震动 recordTime=SystemClock.elapsedRealtime()-clock.getBase(); } } private class BaseListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub recordTime=0;//重置时间 GameStart.this.clock.setBase(SystemClock.elapsedRealtime());//复位 } }
计时功能完成
效果如图:
========================================================
【倒计时】:
采用一个CountDownTimer
同样准备一个textview用于显示
代码如下:
public void initCountDownTimer(long millisInFuture){ timer = new CountDownTimer(millisInFuture, 1000) { @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub lefttime=millisUntilFinished; cdtxt.setText(millisUntilFinished/1000/60+":"+millisUntilFinished/1000%60); } @Override public void onFinish() { cdtxt.setText("00:00"); } }; }
开始倒计时后,只有暂停功能
private class StartListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(timer!=null){ Toast.makeText(getBaseContext(),lefttime+"", 100).show(); timer.start(); start.setVisibility(View.GONE); //设置开始按钮隐藏 } } } private class StopListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(timer!=null&&!isPause){ Toast.makeText(getBaseContext(),"pause", 100).show(); isPause=true; timer.cancel(); } else if(lefttime!=0){ Toast.makeText(getBaseContext(),"continue", 100).show(); initCountDownTimer(lefttime); timer.start(); isPause=false; } } }
实现效果如下:
相关推荐
CXsilent 2020-09-16
lanzhusiyu 2020-07-18
小科的奋斗 2020-06-14
qidiantianxia 2020-06-14
MichelinMessi 2020-06-13
sfkong 2020-06-11
happinessaflower 2020-06-06
kidneybeans 2011-03-15
jiangbo0 2019-09-08
snowfoxmonitor 2015-03-12
zhouboxiao 2016-05-26
NONINETEEN 2016-06-14
健康学习 2014-02-24
富贵 2013-06-06
梦秋雨 2012-08-08
aaalk00 2012-06-26
zfjdoreen 2011-04-18
luoshen 2011-01-21
rookieding 2018-05-25