英文:
How can i reset the timer if my button is clicked?
问题
我有一个TextView,并且我制作了一个计时器,从45秒倒数到0。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView)findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if(millisUntilFinished <= 10000){
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
}
我希望在时间到期后,给用户一个选项,让用户点击b1按钮重新启动计时器。
英文:
I have a textview and i made a timer to decrease from 45 seconds to 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView)findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if(millisUntilFinished<=10000){
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
I want after the time has expired give the option for the user to click the b1 button and restart the timer
答案1
得分: 0
你可以创建CountDownTimer
的实例,然后在需要重新启动时,可以对该实例执行cancel
和start
操作。
CountDownTimer countDownTimer = new CountDownTimer(45000, 1000) {
//......................
}
//首次启动
countDownTimer.start();
//当按钮被点击时重新启动
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countDownTimer.cancel();
countDownTimer.start();
}
});
英文:
You can create instance of CountDownTimer
, then when you need to restart you can perform cancel
and start
on that instance.
CountDownTimer countDownTimer = new CountDownTimer(45000, 1000) {
//......................
}
//Start at first
countDownTimer.start();
//Restart when button is pressed
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countDownTimer.cancel();
countDownTimer.start();
}
});
答案2
得分: 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView) findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(view -> restartTimer());
restartTimer();
}
private void restartTimer() {
b1.setEnabled(false);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if (millisUntilFinished <= 10000) {
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
}
英文:
Is this what you want?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView) findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(view -> restartTimer());
restartTimer();
}
private void restartTimer() {
b1.setEnabled(false);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if (millisUntilFinished <= 10000) {
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论