我不知道爲什麼最後一個刻度線不起作用,但您可以使用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()。
EDIT-2愚蠢的問題
居然有上停止計數器沒有錯誤,但後停止(恢復)出現在啓動的錯誤一次。
我正在寫一個新的更新完整的代碼,我剛剛嘗試過,它的工作。這是一個基本的計數器,通過啓動和停止按鈕在屏幕上顯示時間。
類櫃檯
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>
我想我應該只是使用處理程序來照顧它,但減去我在我的問題中討論的缺陷,CountDownTimer非常簡單和完美!我的意思是從您的代碼示例和我的代碼中,您可以看到CountDownTimer與Handler相比簡單而緊湊!如果只是它實際上正常工作,雖然哈哈。 – ProgrammingMakesMeQQ 2012-01-15 23:18:23
絕對你是對的,我只是想表明你可以用其他方法做到這一點。 – ocanal 2012-01-16 00:46:29
@ocanal謝謝我使用你的代碼,但如果我想停止計時器如何做...請回復 – Goofy 2012-03-26 10:25:09