gpt4 book ai didi

java - 切换按钮启动 Timer1 和 Timer2 但仅停止 Timer1

转载 作者:行者123 更新时间:2023-12-01 10:55:51 25 4
gpt4 key购买 nike

我试图说服切换按钮在单击“启动计时器”时启动计时器 1 和计时器 2,并且只停止计时器 1,而计时器 2 继续“停止计时器”我的主要 Activity 如下所示:

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);
}

目标是当单击切换“启动计时器”时,两个计时器(chrononeter1 和 chronometer2)都应启动,而当单击切换“停止计时器”时,仅 chronometer1 应停止。请指教!谢谢!

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>

最佳答案

好的,查看你的代码有一段时间了。你可以使用天文台表。喜欢:

//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);

然后在切换按钮中启动它们:

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

然后当 passTog.isChecked() = false 时只调用timer1.stop()。第二个计时器将继续。

换句话说,当您的切换被选中时,在两个计时器上调用 .start() ;而当您的切换未被选中时,仅在一个计时器上调用 .stop() 。

我没有看到“停止计时器”代码。

关于java - 切换按钮启动 Timer1 和 Timer2 但仅停止 Timer1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com