gpt4 book ai didi

java - 在 Android 中创建单例 CountDownTimer

转载 作者:行者123 更新时间:2023-11-30 11:24:17 26 4
gpt4 key购买 nike

我是 android 的初学者,我写了一个 Activity 。它包含一个从特定值开始倒计时的 CountDownTimer。它还包含一个加载文本信息的按钮和一个显示计数的 TextView 。

下面是 Activity1 的代码:

public class Screen extends Activity1 implements OnClickListener {  
private static final int MILLIS_PER_SECOND = 1000;
private static final int SECONDS_TO_COUNTDOWN = 1;
TextView Time;
int totaltime;
Button startTimer, howTo, pause;
protected CountDownTimer MyTimer;
int PracticeCount;
long tot;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pushupscreen);
getRefs();
getSpeed();
getCount();
setTotalTime();
startTimer.setOnClickListener(this);
pause.setOnClickListener(this);
}

private void getRefs() {
// Initialize layout resources
Time = (TextView) findViewById(R.id.tvTime);

startTimer = (Button) findViewById(R.id.bStart);
howTo = (Button) findViewById(R.id.btHowTo);
pause = (Button) findViewById(R.id.bPause);
howTo.setOnClickListener(this);

}

private void getTheCount() {
//get count from SharedPreferences
}



private void getSpeed() {
//get speed from SharedPreferences
}

private void setCount(){
totalTime=speed*count;}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == startTimer) {

try {
showTimer(time);

} catch (NumberFormatException e) {
// method ignores invalid (non-integer) input and waits
// for something it cant use
}
} else if (v == pause) {
MyTimer.cancel();
Timer.setText("Resume");
} else if (v == howTo) {

//Launch screen containing information
}
}

private void showTimer(long time) {
if (MyTimer != null) {
MyTimer.cancel();
}

MyTimer = new CountDownTimer(tot2, MILLIS_PER_SECOND) {
@Override
public void onTick(long millisUntilFinished) {
tot = millisUntilFinished;
long seconds = millisUntilFinished / 1000;
Time.setText(String.format("%02d", seconds / 60) + ":"
+ String.format("%02d", seconds % 60));

}

@Override
public void onFinish() {
Time.setText("KABOOM!");

}
}.start();
}

这里是布局文件:

<TextView
android:id="@+id/tvTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:padding="10dip"
android:text="@string/starttime"
android:textSize="60sp" />

<Button
android:id="@+id/bStart"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="@+id/tvTime"
android:text="Start" />

<Button
android:id="@+id/bPause"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="@+id/tvTime"
android:layout_toRightOf="@+id/btHowTo"
android:text="Pause" />

<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btHowTo"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="25"
android:textSize="80sp"
android:textAlignment="center"/>

我的问题:

1.如何创建 4 个使用相同布局和相同计时器的 Activity ?每个 Activity 在 TextView 中加载不同的内容,并在单击 HowTo 按钮时加载不同的屏幕。
2.如何将Activity1设计为运行时间设置的1/4并将剩余时间传递给Activity2?是否可以?

如果您能提供任何帮助和建议,我将不胜感激。谢谢。

最佳答案

这里有几件事。

  1. 重用布局非常容易。在每个 Activity 的 onCreate 中,您只需调用:setContentView(R.layout.pushupscreen); pushupscreen.xml 文件可以通过这种方式在所有 Activity 之间共享。

  2. 您可能想要做的是将时间戳保存到所有 Activity 的一些公共(public)数据源。这可能是对 SharedPreferences 文件的写入:Documentation here .然后,随着每个 Activity 的恢复,通过将此时间戳与当前时间戳进行比较来检查已经过去了多少时间。您还可以在启动后续 Activity 的 Intent 中将时间戳作为额外信息传递。可以找到相关文档 herehere

关于java - 在 Android 中创建单例 CountDownTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712668/

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