简体   繁体   中英

Toggle button to start Timer1 and Timer2 but only stop Timer1

I'm trying to convince a Toggle button to start Timer1 and Timer2 when "Start Timer" is clicked and only stop Timer1 leaving Timer2 continuing on "Stop Timer" My main Activity looks like this:

 public class MainActivity extends AppCompatActivity { long init, now, paused, millis, seconds, minutes, hours; TextView display; TextView display2; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); handler = new Handler(); display = (TextView) findViewById(R.id.chronometer1); final ToggleButton passTog = (ToggleButton) findViewById(R.id.toggleButton1); final Runnable updater = new Runnable() { @Override public void run() { if (passTog.isChecked()) { now = System.currentTimeMillis(); millis = now - init; seconds = ((millis / 1000) % 60); minutes = (millis / 1000) / 60; hours = (minutes / 3600000); String hh = hours < 10 ? "0" + hours : hours + ""; String mm = minutes < 10 ? "0" + minutes : minutes + ""; String ss = seconds < 10 ? "0" + seconds : seconds + ""; display.setText(hh + ":" + mm + ":" + ss); handler.postDelayed(this, 10); continueTimer(); } } }; passTog.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { init = System.currentTimeMillis(); handler.post(updater); } }); } @Override protected void onPause() { super.onPause(); paused = System.currentTimeMillis(); } @Override protected void onResume() { super.onResume(); init += System.currentTimeMillis() - paused; } public void continueTimer() { display2 = (TextView) findViewById(R.id.chronometer2); now = System.currentTimeMillis(); millis = now - init; seconds = ((millis / 1000) % 60); minutes = (millis / 1000) / 60; hours = (minutes / 3600000); String hh = hours < 10 ? "0" + hours : hours + ""; String mm = minutes < 10 ? "0" + minutes : minutes + ""; String ss = seconds < 10 ? "0" + seconds : seconds + ""; display2.setText(hh + ":" + mm + ":" + ss); } 

The Goal is when toggle "START TIMER" is clicked both TIMERS (chrononeter1 and chronometer2) should start and when toggle "STOP TIMER" is clicked only chronometer1 should stop. Please Advise! Thanks!

XML looks Like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="74dp"
        android:fontFamily="sans-serif-light"
        android:format="00:00:00"
        android:textSize="48sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/contraction_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time since last start timer:" />

        <Chronometer
            android:id="@+id/chronometer2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:format="00:00:00"
            android:textSize="12sp" />

    </LinearLayout>

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-medium"
        android:textOff="Start Timer"
        android:textOn="Stop Timer"

        />

</LinearLayout>

Ok, been looking at your code awhile. You could use Chronometer. Like:

//Decalare and initialize timer1.
private Chronometer timer1;
timer1 = (Chronometer)v.findViewById(R.id.chronometer1);

//Declare and initialize timer2.  
private Chronometer timer2;
timer2 =(Chronometer)v.findViewById(R.id.chronometer2);

Then start them in your toggle button:

@Override
public void run() {
    if (passTog.isChecked()) {
        //start chronometers
        timer1.start();
        timer2.start();
    }

Then when when passTog.isChecked() = false only call timer1.stop(). The 2nd timer will continue.

In other words call .start() on both timers when your toggle is checked and only call .stop() on one timer when your toggle is unchecked.

I didn't see the "Stop Timer" code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM