如何在java中创建倒计时秒表
调用函数()时,如果单击按钮,我希望有机会取消SOS调用。 基本上它读取的是如何在java中创建倒计时秒表,java,android,Java,Android,调用函数()时,如果单击按钮,我希望有机会取消SOS调用。 基本上它读取的是Integer.parseInt(cancelTime.getText().toString()),它有一秒的时间来取消 我的问题是,我试图将从Integer.parseInt(cancelTime.getText().toString())到0的时间倒数显示为一个巨大的数字:例如:105468261 private void function() { startTime = System.curren
Integer.parseInt(cancelTime.getText().toString())
,它有一秒的时间来取消
我的问题是,我试图将从Integer.parseInt(cancelTime.getText().toString())
到0的时间倒数显示为一个巨大的数字:例如:105468261
private void function()
{
startTime = System.currentTimeMillis();
Runnable runnable = new Runnable() {
@Override
public void run() {
double elapsedTime = (System.currentTimeMillis() - startTime)/1000;
alertButton.setText("CANCEL THE SOS: " + (int)elapsedTime);
alertButton.setBackgroundColor(Color.parseColor("#FF0000"));
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, Integer.parseInt(cancelTime.getText().toString()));
}
我已经修改了你的代码,这可能会对你有所帮助
private void function()
{
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
alertButton.setText("seconds remaining: " + millisUntilFinished / 1000);
alertButton.setBackgroundColor(Color.parseColor("#FF0000"));
}
public void onFinish() {
alertButton.setText("Cancel SOS");
}
}.start();
}
根据需要修改新的倒计时(300001000)
参数1:-long millissinfuture
参数2:-长倒计时间隔您需要使用
long
s,而不是int
s。您的意思是什么?我只想显示3秒,而不是3.0213912931239120391尝试格式化/舍入。这是一个类似问题的链接,cancelTime
设置在哪里?您似乎在延迟结束后调用runnable?