如何在java

时间:2017-06-23 08:25:42

标签: java android

当我调用函数()时,我希望有机会取消SOS调用,如果我单击按钮。 基本上它会读取Integer.parseInt(cancelTime.getText().toString()),其中有一段时间可以取消。

我的问题是,我试图显示从Integer.parseInt(cancelTime.getText().toString())到0的时间倒计时,它显示为一个巨大的数字:例如:10546468261

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()));
}

1 个答案:

答案 0 :(得分:0)

我修改了您的代码,这可能对您有帮助。

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();
}

根据需要修改新的CountDownTimer(30000,1000)

参数1: - long millisInFuture

参数2: - long countDownInterval