Witam. Mam do napisania program "stoper" na androida. Napisałem do momentu dodawania drugiej pozycyji i nie wiem jak się do tego zabrać dalej. Gdbyby ktoś mnie trochę nakierował, będę niezmiernie wdzięczny.
Polecenie zadania:
Program ma za zadanie odliczanie czasu. Przy kliknięciu na przycisk
START licznik zaczyna pracę. Przyciśnięcie przycisku STOP zatrzymuje pracę
licznika. Przycisk CZYSC czyści wskazanie ekranu i pozwala wyświetlić tylko
pierwszą pozycję, kolejne pozycje dodawane są przyciskiem NOWY.
Maksymalnie można wyświetlić 6 wskazań.
Jak na razie udało mi się napisać to, co przedstawiam poniżej :
package com.example.zad1_stoperr;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button start;
Button stop;
Button nowy;
Button wyzeruj;
TextView czas1;
TextView czas2;
TextView time3;
private long start_czas1 = 0L;
private long start_czas2 = 0L;
private Handler customHandler = new Handler();
long milisekundy = 0L;
long bufor_czasu = 0L;
long update_czasu = 0L;
boolean stop_czas = false;
long milisekundy2 = 0L;
long bufor_czasu2 = 0L;
long update_czasu2 = 0L;
boolean stop_czas2 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.startButton);
stop = (Button) findViewById(R.id.pauseButton);
czas1 = (TextView) findViewById(R.id.timerValue);
czas2 = (TextView) findViewById(R.id.timerValue2);
time3 = (TextView) findViewById(R.id.timerValue3);
nowy = (Button) findViewById(R.id.button1);
wyzeruj = (Button) findViewById(R.id.button2);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startButton:
start_czas1 = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
break;
case R.id.pauseButton:
bufor_czasu += milisekundy;
customHandler.removeCallbacks(updateTimerThread);
bufor_czasu2 += milisekundy2;
customHandler.removeCallbacks(updateTimerThread2);
break;
}
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
milisekundy = SystemClock.uptimeMillis() - start_czas1;
update_czasu = bufor_czasu + milisekundy;
int secs = (int) (update_czasu / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (update_czasu % 1000);
String localtime = "" + mins + ":" + String.format("%02d", secs)
+ ":" + String.format("%03d", milliseconds);
czas1.setText(localtime);
if (!stop_czas)
customHandler.postDelayed(this, 0);
}
};
private Runnable updateTimerThread2 = new Runnable() {
public void run() {
milisekundy2 = SystemClock.uptimeMillis() - start_czas2;
update_czasu2 = bufor_czasu2 + milisekundy2;
int secs = (int) (update_czasu2 / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (update_czasu2 % 1000);
String localtime = "" + mins + ":" + String.format("%02d", secs)
+ ":" + String.format("%03d", milliseconds);
czas2.setText(localtime);
if (!stop_czas)
customHandler.postDelayed(this, 0);
}
};
public void wyzeruj(View v)
{
czas2.setVisibility(View.INVISIBLE);
time3.setVisibility(View.INVISIBLE);
czas1.setText("00:00:00");
czas2.setText("00:00:00");
bufor_czasu=0;
bufor_czasu2=0;
}
public void nowy_btn(View arg0)
{
{
{
switch (arg0.getId()) {
case R.id.button1:
czas2.setVisibility(View.VISIBLE);
start_czas2 = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread2, 0);
break;
case R.id.pauseButton:
bufor_czasu2 += milisekundy2;
customHandler.removeCallbacks(updateTimerThread2);
break;
}
}
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<Button
android:id="@+id/pauseButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/startButton"
android:layout_alignBottom="@+id/startButton"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:text="pause" />
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startButton"
android:layout_below="@+id/startButton"
android:layout_marginLeft="15dp"
android:layout_marginTop="47dp"
android:text="00:00:000"
android:textColor="#ffffff"
android:textSize="40sp"
android:visibility="visible" />
<Button
android:id="@+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="23dp"
android:text="start" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pauseButton"
android:layout_below="@+id/startButton"
android:onClick="nowy_btn"
android:text="Nowy" />
<TextView
android:id="@+id/timerValue2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timerValue"
android:layout_below="@+id/timerValue"
android:text="00:00:000"
android:textColor="#ffffff"
android:textSize="40sp"
android:visibility="invisible" />
<TextView
android:id="@+id/timerValue3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timerValue2"
android:layout_below="@+id/timerValue2"
android:text="00:00:000"
android:textColor="#ffffff"
android:textSize="40sp"
android:visibility="invisible" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startButton"
android:layout_below="@+id/startButton"
android:onClick="wyzeruj"
android:text="Czysc" />
</RelativeLayout>