gpt4 book ai didi

android - 如何用秒和毫秒进行倒计时

转载 作者:行者123 更新时间:2023-12-02 06:27:48 27 4
gpt4 key购买 nike

我正在与 android studio 合作开发一个从 10 秒开始倒计时的应用程序。我编写了代码,它工作正常,但它只显示剩余的秒数,现在我希望倒计时也显示毫秒。你能帮我吗?这是代码

public class MainActivity extends Activity {
TextView txtCount;
Button btnCount;
int count = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);

btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
final TextView textic = (TextView) findViewById(R.id.textView2);

CountDownTimer Count = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));

textic.setText(seconds + "seconds " + millisUntilFinished / 1000);

}

public void onFinish() {
textic.setText("TEMPO SCADUTO");
}
};

Count.start();



}

最佳答案

//start button click
CountDown timer = new CountDown(180000, 1000);
timer.start();

//stop button click
timer.stop();

//countdown class
public class CountDown extends CountDownTimer {

public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {
long ms = millisUntilFinished;
String text = String.format("%02d\' %02d\"",
TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
textViewTimer.setText(text);
}

@Override
public void onFinish() {
textViewTimer.setText("ffinish");
}
}

关于android - 如何用秒和毫秒进行倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060466/

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