我是一名Android初学者,我写了一个活动。它包含一个从特定值开始倒数的CountDownTimer。它还包含一个加载文本信息的按钮和一个显示计数的文本视图。在Android中创建Singleton CountDownTimer
下面是活动1中的代码:
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个使用相同布局和相同定时器的活动?每个活动在文本视图中加载不同的内容,并在单击“按钮”按钮时加载不同的屏幕。
2.如何将Activity1设计为运行时间的1/4并将剩余时间传递给Activity2?可能吗?
我真的很感谢您提供的任何帮助和建议。谢谢。
请注意,您需要创建一个Bundle(http://developer.android.com/reference/android/os/Bundle.html),并将时间戳添加到它。 – akhalsa