2017-05-27 103 views
0

这是我第一次使用Android Studio,而且我的Java技能基本上都不存在,因此我需要一个与我正在开发的项目有关的帮助。 我需要制定一个计时器,重复一定的休息后。用户应该决定定时器重复的次数以及中断的持续时间。 到目前为止,我做的标准倒数计时器,但无论我怎么努力,我不能让它重复如何使倒数计时器重复

这是工作的代码我到目前为止 接口:

<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" 
tools:context="com.example.asus.timer.MainActivity" 
android:padding="20dp" 
android:orientation="vertical" 
> 


<EditText 
    android:id="@+id/editTextSession" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:padding="20dp" 
    android:hint="Session (seconds)" 
    android:textAlignment="center" 
    /> 

<EditText 
    android:id="@+id/editTextBreak" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:padding="20dp" 
    android:hint="Break (seconds)" 
    android:textAlignment="center" 
    /> 

<EditText 
    android:id="@+id/editTextRepeats" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:padding="20dp" 
    android:hint="Repeats" 
    android:textAlignment="center" 
    /> 

<Button 
    android:id="@+id/buttonStart" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Start!" 
    android:padding="20dp" 
    /> 

<TextView 
    android:id="@+id/textViewTimer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:padding="20dp" 
    android:textAlignment="center" 
    android:textSize="60dp" 
    android:paddingTop="40dp" 
    android:gravity="center" 
    /> 

功能

public class MainActivity extends AppCompatActivity { 

EditText editTextSession; 
EditText editTextBreak; 
EditText editTextRepeats; 
Button buttonStart; 
TextView textViewTimer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editTextSession =(EditText) findViewById(R.id.editTextSession); 
    editTextBreak =(EditText) findViewById(R.id.editTextBreak); 
    editTextRepeats =(EditText) findViewById(R.id.editTextRepeats); 
    buttonStart =(Button) findViewById(R.id.buttonStart); 
    textViewTimer =(TextView) findViewById(R.id.textViewTimer); 

    buttonStart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String text = editTextSession.getText().toString(); 
      int seconds = Integer.valueOf(editTextSession.getText().toString()); 
      CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000) { 
       @Override 
       public void onTick(long millis) { 
        textViewTimer.setText("time:" + (int) (millis/1000)); 
       } 

       @Override 
       public void onFinish() { 
        extViewTimer.setText("Finished!"); 
       } 
      }.start(); 
     } 
    } 
); 

回答

0

如果您正在寻找另一种创建计时器的方式,您可以使用Runnable()

private TextView timeCounterTextView; 
private int smsTimeOut = 10; // 10 SECONDS 
private Runnable timeCounterRunnable = new Runnable() { 
    @Override 
    public void run() { 

     if (smsTimeOut == 1) { 
      //Do whatever when timeout 
     } 
     smsTimeOut--; 
     timeCounterTextView.setText(String.valueOf(smsTimeOut)); 
     handler.postDelayed(this, 1000); 
    } 
}; 

,只要你想开始倒计时初始化和像下面init()方法启动定时器:要重复可以再次调用init()

private void init(){ 
    handler.removeCallbacks(timeCounterRunnable); 
    smsTimeOut = 10; 
    handler = new Handler(); 
    handler.post(timeCounterRunnable); 
} 

随时随地。

希望这有助于。

0

您是否尝试使用Integer.parseInt(String value)而不是Integer.valueOf(String value)?也许它会有所作为。如果有人输入了错误的值,你可以添加一个异常。

0

使用此

timer.start(); 

而且随着停止:

timer.cancel(); 

例子:

String text = editTextSession.getText().toString(); 
int seconds = Integer.valueOf(text); 

//get how many repeat 
String repeatText = editTextSession.getText().toString(); 
int repeat = Integer.valueOf(repeatText); 

int count= 0; 

CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000) { 
    @Override 
    public void onTick(long millis) { 
     textViewTimer.setText("time:" + (int)(millis/1000)); 
    } 

    @Override 
    public void onFinish() { 
     extViewTimer.setText("Finished!"); 
     count++; 
     //check if count still less than repeat number 
     if(count < repeat){ 
      Runnable r = new Runnable() { 
        public void run() { 
         countDownTimer.start(); 
        }}; 

      new Handler().postDelayed(r,2000); //delay repeat timer 2 seconds 

     } 
     else{ 
      countDownTimer.cancel(); 
      count = 0; 
     } 
    } 
}.start(); 
+0

ZeroOne,你有一个建议,如何添加重复之间的间隔? –

+0

你想延迟重复的过程吗? – ZeroOne