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; } } }
实现效果如下:
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28