Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中创建倒计时秒表_Java_Android - Fatal编程技术网

如何在java中创建倒计时秒表

如何在java中创建倒计时秒表,java,android,Java,Android,调用函数()时,如果单击按钮,我希望有机会取消SOS调用。 基本上它读取的是Integer.parseInt(cancelTime.getText().toString()),它有一秒的时间来取消 我的问题是,我试图将从Integer.parseInt(cancelTime.getText().toString())到0的时间倒数显示为一个巨大的数字:例如:105468261 private void function() { startTime = System.curren

调用函数()时,如果单击按钮,我希望有机会取消SOS调用。 基本上它读取的是
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?