我正在尝试创建一个应用程序,您可以在其中输入会话的长度,然后会话数量会倒计时会话的长度,发出哔哔声,然后如果仍有会话但会自动开始新的倒计时但是此时,应用程序会同时启动所有倒计时。
因此,如果有3个会话每个30秒长,它会将计数器从30减少到每秒3到-60。
在这种情况下,我试图让它做的是,从30开始倒计时到0,发出哔哔声,然后开始第二次倒计时从30到0哔声然后 从30点到0点开始第3次倒计时。
这是manActivity.Java
package uk.co.test.fitnesstimer;
import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static uk.co.test.fitnesstimer.R.id.countdownTimer;
import static uk.co.test.fitnesstimer.R.id.session_length;
public class MainActivity extends AppCompatActivity {
public int counter;
public int sessionsLeft;
Button button;
TextView textView;
public int lengthOfActivities = 30;
public int numOfActivities = 1;
public int isActive = 0;
public void increment(View view) {
lengthOfActivities++;
display(lengthOfActivities);
}
public void decrement(View view) {
if (lengthOfActivities > 1) {
lengthOfActivities--;
}
display(lengthOfActivities);
}
public void incrementNumOfActivities(View view) {
numOfActivities++;
displayNum(numOfActivities);
}
public void decrementNumOfActivities(View view) {
if (numOfActivities > 1) {
numOfActivities--;
}
displayNum(numOfActivities);
}
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.session_length);
quantityTextView.setText(String.valueOf(number));
}
private void displayNum(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.numOfActivities);
quantityTextView.setText(String.valueOf(number));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(countdownTimer);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if (isActive == 0){
isActive = 1;
sessionsLeft = numOfActivities;
counter = lengthOfActivities;
while (sessionsLeft > 0) {
new CountDownTimer((lengthOfActivities * 1000), 1000){
public void onTick(long millisUntilFinished){
textView.setText(String.valueOf(counter));
//if (counter > 0) {
counter = counter - 1;
//}
}
public void onFinish(){
textView.setText("FINISH!!");
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,150);
isActive = 0;
}
}.start();
sessionsLeft--;
}
}
else {
Context context = getApplicationContext();
CharSequence text = "Timer is running already!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
});
}
}
这是ActivityMain XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="uk.co.test.fitnesstimer.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Length Of Sessions:"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="1"
android:onClick="decrement" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/session_length"
android:text="30"
android:gravity="center"
android:layout_weight="1" />
<Button
android:layout_width="1dp"
android:layout_height="wrap_content"
android:text="+"
android:onClick="increment"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number Of Sessions:"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="1"
android:onClick="decrementNumOfActivities" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/numOfActivities"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="+"
android:layout_weight="1"
android:onClick="incrementNumOfActivities" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:onClick="submitOrder"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/countdownTimer" />
</LinearLayout>
答案 0 :(得分:1)
如果你想连续调用你的倒计时,你应该写一个递归函数而不是一段时间..这会在每次倒计时后自行调用
将您编写的所有逻辑放在onClick函数中,并使其自行调用递归直到所有会话都完成。
private void countDown()
{
counter = lengthOfActivities;
if (sessionsLeft > 0) {
new CountDownTimer((lengthOfActivities * 1000), 1000)
{
public void onTick(long millisUntilFinished){
textView.setText(String.valueOf(counter));
counter = counter - 1;
}
public void onFinish(){
textView.setText("FINISH!!");
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,150);
isActive = 0;
sessionsLeft--;
countDown();
}
}.start();
}
}
// onclick
if (isActive == 0){
isActive = 1;
sessionsLeft = numOfActivities;
countDown();
}