2017-05-27 206 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