简体   繁体   English

切换按钮以启动Timer1和Timer2,但仅停止Timer1

[英]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: 我试图说服Toggle按钮在单击“启动计时器”时启动Timer1和Timer2,并且仅停止Timer1,而使Timer2继续在“停止计时器”上我的主要活动如下所示:

 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. 目标是单击“ START TIMER”开关时,两个计时器(chrononeter1和chronometer2)都应启动,而单击“ STOP TIMER”开关时,只有chronometer1应该停止。 Please Advise! 请指教! Thanks! 谢谢!

XML looks Like this: XML看起来像这样:

<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(). 然后,当passTog.isChecked()= false时,仅调用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. 换句话说,当您的切换被选中时,在两个计时器上都调用.start(),而当您的切换未选中时,仅在一个计时器上调用.stop()。

I didn't see the "Stop Timer" code. 我没有看到“停止计时器”代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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