gpt4 book ai didi

java - 试图制作一个定时器循环

转载 作者:行者123 更新时间:2023-11-30 03:06:45 26 4
gpt4 key购买 nike

我正在尝试制作一个倒计时时间循环。我想让它循环直到“int x”达到 100。我试过添加一个 do/while 和一个 for 循环,但我认为我做错了。有什么建议么?感谢您的关注。 :-)`

    public class MainActivity extends Activity{

Button buttonStartTime;

// clicking this button will start time count down
TextView textViewShowTime; // will show the time
TextView shots;
CountDownTimer countDownTimer; // built in android class CountDownTimer
long totalTimeCountInMilliseconds; // total count down time in milliseconds
long timeBlinkInMilliseconds; // start time of start blinking
boolean blink; // controls the blinking .. on and off
static int x = 3;

int whole = 100;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getReferenceOfViews (); // get all views
setActionListeners (); // set action listerns

totalTimeCountInMilliseconds = 60 * 1000; // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = 10 * 1000; // blink starts at 1 minutes = 60 seconds

}



private void setActionListeners() {

buttonStartTime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {



textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);



countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500 milliseconds

@Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;

if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
// change the style of the textview .. giving a red alert style

if ( blink ) {
textViewShowTime.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime.setVisibility(View.INVISIBLE);
}

blink = !blink; // toggle the value of blink
}

textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}

@Override
public void onFinish() {
x++;
// this function will be called when the timecount is finished
textViewShowTime.setText("SHOT!!!");
textViewShowTime.setVisibility(View.VISIBLE);


}

}


.start();


}


});


}



private void getReferenceOfViews() {

buttonStartTime = (Button) findViewById(R.id.btnStartTime);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
}

}`

最佳答案

totalTimeCountInMilliseconds =1000 * 1000;      // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = 10 * 1000; // blink starts at 1 minutes = 60 seconds



new CountDownTimer(totalTimeCountInMilliseconds , timeBlinkInMilliseconds )

上面会调用 onTick 100 次

关于java - 试图制作一个定时器循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21681851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com