[英]set different times android countdown timer
So it has been a while since I have tried to program Android. 自从我尝试对Android进行编程以来已经有一段时间了。 Now I have a better understanding of java than I have in the past, but my question is how can I limit the number of times a button is clicked. 现在,我对Java的理解比过去更好,但是我的问题是如何限制按钮被单击的次数。 The context of this is that I have a timer the timer changes each time the old one has expired. 这样做的上下文是我有一个计时器,每次旧计时器到期时,计时器都会更改。 So I have created an Array of times, such as {3,4,7,1,3} but once it hits the end I don't want a toast to pop up. 所以我创建了一个时间数组,例如{3,4,7,1,3},但是一旦结束,我就不想吐司了。 I know how to do the toast part and so right now I have my CountDownTimer(Array[i],0). 我知道如何做吐司部分,所以现在我有了CountDownTimer(Array [i],0)。 and in the onFinish method that is provided I index i one more time. 并在提供的onFinish方法中再索引一次。 But I don't want to call upon this method if i>Array[].lenghth. 但是,如果i> Array []。lenghth,我不想调用此方法。 When I try to add just a simple conditional statement like that before I create a new CountDownTimer, I get an error that involves closing off my methods and conditionals appropriately. 当我尝试在创建新的CountDownTimer之前仅添加一个简单的条件语句时,出现一个错误,该错误涉及适当地关闭我的方法和条件。 Below is what I have. 以下是我所拥有的。 Thank you guys in advance! 预先谢谢你们!
package com.example.countdown;
import java.util.ArrayList;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private int[] Times= new int[];
Times={4,5,3,2,6}
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
if(index<Times.size()){
public void onClick(View v){
CountDownTimer timer = new CountDownTimer(
Times[i]), 0) {
TextView textview =(TextView) findViewById(com.example.countdowntest.R.id.textView);
public void onTick(long millisUntilFinished) {textview.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
textview.setText("done!");
index++;
}
}.start();
}
});
}
}
}
在onClick
方法内移动if(index<Times.size()){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.