gpt4 book ai didi

java - 24 小时倒计时器,具有小时、分钟、秒三个独立的 TextView

转载 作者:行者123 更新时间:2023-12-01 06:15:52 25 4
gpt4 key购买 nike

我正在尝试实现一个 24 小时倒计时器,其中三个独立的 TextView 一个保存小时,一个保存分钟,一个保存秒。

enter image description here

按下开始时间按钮后,我希望计时器倒计时。

我对此做了很多研究,并尝试实现许多示例,从文档中学习并根据自己的喜好进行修改,但我无法给出可靠的解决方案。

Countdown timer in HH:MM:SS format in Android

Display timer in textview contain days, hours, minutes and seconds in android

问题是,对于android中的倒计时器来说,它通常只用一个 TextView 来实现,将其视为带有时间的长字符串,然后从那里开始倒计时。我不确定在我的例子中如何处理 24 小时倒计时器,因为我有三个单独的 TextView 。

我很惊讶我找不到太多关于这个问题的信息。

即:

例如,我有一个倒计时器,但这仅使用一个 TextView 。我将如何制作一些东西来考虑我的三个单独的 TextView (小时、分钟和秒)并相应地倒计时。

    Button buttonStartTime;                 // clicking this button will start time count down 
TextView textViewShowTime; // will show the time
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

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

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

totalTimeCountInMilliseconds = 60 * 1000; // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = 30 * 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() {
// this function will be called when the timecount is finished
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
}

}.start();

}
});
}

private void getReferenceOfViews() {

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

编辑:现在我有这个:

private void setActionListeners() {

start_timer.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) {
int seconds = leftTimeInMilliseconds / 1000 % 60;
int minutes = leftTimeInMilliseconds / 60000 % 60;
int hours = leftTimeInMilliseconds / 3600000;




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

if ( blink ) {
number_text.setVisibility(View.VISIBLE);
minute_text.setVisibility(View.VISIBLE);
second_text.setVisibility(View.VISIBLE);


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


}

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

number_text = String.format("%02d", seconds % 60);
minute_text = String.format("%02d", seconds / 60);
second_text = String.format("%02d", seconds / 3600); // format the textview to show the easily readable format
}

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

}.start();

}
});
}

private void getReferenceOfViews() {

start_timer = (Button) findViewById(R.id.start_button);
number_text = (TextView) findViewById(R.id.hour_progress_number);
minute_text = (TextView) findViewById(R.id.minute_progress_number);
second_text = (TextView) findViewById(R.id.second_progress_number);


}

我收到这些错误。

enter image description here

enter image description here

最佳答案

在 onTick() 中而不是:

textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));

做:

textViewSeconds.setText(String.format("%02d", seconds % 60));
textViewMinutes.setText(String.format("%02d", seconds / 60));
textViewHours.setText(String.format("%02d", seconds / 3600));

关于java - 24 小时倒计时器,具有小时、分钟、秒三个独立的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946476/

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