码:
public class SMH extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); new CountDownTimer(10000, 2000) { public void onTick(long m) { long sec = m/1000+1; tv.append(sec+" seconds remain\n"); } public void onFinish() { tv.append("Done!"); } }.start(); }
输出: 剩余10秒剩余 8秒剩余 6秒剩余 4秒剩余 完成!
问题:
如何显示“ 剩余2秒 ”?经过的时间确实是10秒,但是最后一个onTick()永远不会发生。如果我将第二个参数从2000更改为1000,则输出为:
剩余10秒剩余 9秒剩余 8秒剩余 7秒剩余 6秒剩余 5秒剩余 4秒剩余 3秒剩余 2秒剩余 完成!
如此看来,似乎跳过了最后一个onTick()调用。顺便说一句,XML文件基本上是默认的main.xml,其中TextView分配了ID tv ,文本设置为“”。
我不知道为什么最后一个刻度不起作用,但是例如可以使用Runable创建自己的计时器。
class MyCountDownTimer { private long millisInFuture; private long countDownInterval; public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) { this.millisInFuture = pMillisInFuture; this.countDownInterval = pCountDownInterval; } public void Start() { final Handler handler = new Handler(); Log.v("status", "starting"); final Runnable counter = new Runnable(){ public void run(){ if(millisInFuture <= 0) { Log.v("status", "done"); } else { long sec = millisInFuture/1000; Log.v("status", Long.toString(sec) + " seconds remain"); millisInFuture -= countDownInterval; handler.postDelayed(this, countDownInterval); } } }; handler.postDelayed(counter, countDownInterval); } }
然后开始
new MyCountDownTimer(10000, 2000).Start();
编辑高飞的问题
您应该有一个变量来保存计数器状态(布尔值)。那么您可以编写Stop()方法,例如Start()。
编辑2高飞的问题
实际上,没有停止计数器的错误,但是在stop(resume)之后再次启动时存在错误。
我正在写一个刚刚尝试过的新的更新的完整代码,它正在工作。这是一个基本的计数器,带有“开始”和“停止”按钮在屏幕上显示时间。
柜台类
public class MyCountDownTimer { private long millisInFuture; private long countDownInterval; private boolean status; public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) { this.millisInFuture = pMillisInFuture; this.countDownInterval = pCountDownInterval; status = false; Initialize(); } public void Stop() { status = false; } public long getCurrentTime() { return millisInFuture; } public void Start() { status = true; } public void Initialize() { final Handler handler = new Handler(); Log.v("status", "starting"); final Runnable counter = new Runnable(){ public void run(){ long sec = millisInFuture/1000; if(status) { if(millisInFuture <= 0) { Log.v("status", "done"); } else { Log.v("status", Long.toString(sec) + " seconds remain"); millisInFuture -= countDownInterval; handler.postDelayed(this, countDownInterval); } } else { Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!"); handler.postDelayed(this, countDownInterval); } } }; handler.postDelayed(counter, countDownInterval); } }
活动课
public class CounterActivity extends Activity { /** Called when the activity is first created. */ TextView timeText; Button startBut; Button stopBut; MyCountDownTimer mycounter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timeText = (TextView) findViewById(R.id.time); startBut = (Button) findViewById(R.id.start); stopBut = (Button) findViewById(R.id.stop); mycounter = new MyCountDownTimer(20000, 1000); RefreshTimer(); } public void StartTimer(View v) { Log.v("startbutton", "saymaya basladi"); mycounter.Start(); } public void StopTimer(View v) { Log.v("stopbutton", "durdu"); mycounter.Stop(); } public void RefreshTimer() { final Handler handler = new Handler(); final Runnable counter = new Runnable(){ public void run(){ timeText.setText(Long.toString(mycounter.getCurrentTime())); handler.postDelayed(this, 100); } }; handler.postDelayed(counter, 100); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"> <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/time"> </TextView> <Button android:text="Start" android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="StartTimer"> </Button> <Button android:text="Stop" android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="StopTimer"> </Button> </LinearLayout>