我是android的初学者,我想制作一个简单的国际象棋计时器时钟。在这里,我有两个按钮。如果我点击第一个按钮,第二个按钮应该开始倒计时,反之亦然。但暂停按钮不能正常工作它只会暂停一次,不会暂停。在这里,我附上了我的代码。
package com.example.jaydeep.practicework.chessClock;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.jaydeep.practicework.R;
public class Main8Activity extends AppCompatActivity {
Button button11,button22;
CountDownTimer count,count1;
int s1=60,s2=60;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
button11 = (Button) findViewById(R.id.Button11);
button22 = (Button) findViewById(R.id.Button22);
button11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button11.setEnabled(false);
reverseTimer1(s2, button22);
}
});
button22.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button22.setEnabled(false);
reverseTimer(s1, button11);
}
});
}
public void reverseTimer(int Seconds,final Button button){
button11.setEnabled(true);
count= new CountDownTimer(Seconds* 1000+1000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
s1=seconds;
int minutes = seconds / 60;
seconds = seconds % 60;
button.setText( String.format("%02d", minutes)
+ ":" + String.format("%02d", seconds));
}
public void onFinish() {
button.setText("Time Up!!!");
}
};
count.start();
}
public void reverseTimer1(int Seconds,final Button button){
button22.setEnabled(true);
count1= new CountDownTimer(Seconds* 1000+1000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
s2=seconds;
int minutes = seconds / 60;
seconds = seconds % 60;
button.setText( String.format("%02d", minutes)
+ ":" + String.format("%02d", seconds));
}
public void onFinish() {
button.setText("Time Up!!!");
}
};
count1.start();
}
}
答案 0 :(得分:0)
cancel();
您需要此代码
if(isPaused || isCanceled)
{
//If the user request to cancel or paused the
//CountDownTimer we will cancel the current instance
cancel();
}
此链接可帮助您了解: https://android--examples.blogspot.com.au/2015/04/android-countdowntimer-start-pause.html