Nie wiem, dlaczego ostatni znacznik nie działa, ale można na przykład utworzyć własny zegar z 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);
}
}
i go uruchomić,
new MyCountDownTimer(10000, 2000).Start();
edycji dla GOOFY Zapytanie
trzeba mieć zmienną do utrzymywania stanu licznika (logiczna). następnie możesz napisać metodę Stop(), taką jak Start().
EDIT-2 dla GOOFY Zapytanie
faktycznie nie ma błędów na zatrzymanie licznika, ale nie jest to problem na początku ponownie po zatrzymaniu (CV).
Piszę nowy zaktualizowany pełny kod, który właśnie wypróbowałem i działa. Jest to podstawowy licznik pokazujący czas na ekranie za pomocą przycisku start i stop.
klasa dla licznika
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);
}
}
klasa aktywność
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>
Pomyślałem, że powinienem użyć Handler'a, żeby się tym zająć, ale bez wady, o której mówię w moim pytaniu, CountDownTimer jest taki prosty i doskonały! Mam na myśli tylko przykład twojego kodu i moje, możesz zobaczyć, jak prosty i kompaktowy CountDownTimer jest porównywany do Handler'a! Jeśli tylko to faktycznie działało dobrze chociaż lol. – ProgrammingMakesMeQQ
absolutnie masz rację, chcę tylko pokazać, że możesz to zrobić za pomocą alternatywnych metod. – ocanal
@ocanal dziękuję używam twojego kodu, ale jeśli chcę zatrzymać timer jak to zrobić ... proszę odpowiedz – Goofy