2013-12-20 40 views
2

我是一名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?可能吗?

我真的很感谢您提供的任何帮助和建议。谢谢。

回答

1

这里有一些东西。

  1. 它很容易重新使用布局。在每个活动的onCreate中,您只需调用: setContentView(R.layout.pushupscreen);可以通过这种方式在所有活动中共享pushupscreen.xml文件。

  2. 您可能想要做的是将时间戳保留到某些常见数据源,以用于所有活动。这可以写入SharedPreferences文件:Documentation here。然后,当每个活动恢复时,通过将此时间戳与当前时间戳进行比较来检查已经过了多少时间。您也可以将时间戳作为额外的来传递,以启动后续活动。该文档可以发现herehere

+0

请注意,您需要创建一个Bundle(http://developer.android.com/reference/android/os/Bundle.html),并将时间戳添加到它。 – akhalsa

0
  1. 你可以做一个自定义的控制,这基本上是一个新的类,它继承了一些其他控件的类(例如的LinearLayout或RelativeLayout的)。然后,您可以将视图的XML加载到新的布局,或以编程方式在控件中创建新的控件。更多的信息在这里: Custom components in Android

  2. 经过1/4的倒计时,你可以创建并发送一个意图在onTick方法中开始一个新的活动。您还可以将余下的3/4作为毫秒值(类型为long)放入另一个意图中。然后,您可以在新活动中获取此值,并在那里调用自定义的CountDownTimer子项以进行倒计时。然后你可以在onFinish()方法完成倒计时之后,最终执行你所希望的。

+0

我希望tvTimer textview显示总倒计时间 - 比如20分钟。这是所有四项活动都要完成的时间。并且在每个活动中,我希望Count的textView在每一个onTick()中都有1/4的时间。我该怎么做呢? – Adarsh