如何使用自定义开始时间实现倒数计时器?

时间:2019-04-30 09:22:01

标签: java android timer countdowntimer

我一直在尝试在简单的文本视图中使用处理程序来实现倒数计时器。我需要设置自定义时间。 例如。,计时器应从 06:12:00

-这是 MainActivity enter code here代码-。

public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button pauseButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

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

     timerValue = (TextView) findViewById(R.id.timerValue);
     startButton = (Button) findViewById(R.id.startButton);

-“开始”按钮监听器-

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

-监听暂停按钮-

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });
}

private Runnable updateTimerThread = new Runnable() {

    public void run()

{

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        int hours = mins/60;
        secs = secs % 60;

        timerValue.setText(String.format("%02d", hours)+":" + 
            (String.format("%02d", mins)) + ":"
                + String.format("%02d", secs) );
        customHandler.postDelayed(this, 0);
    }

};}

我得到的输出在这里https://ibb.co/pd95Yww

2 个答案:

答案 0 :(得分:2)

公共类MainActivity扩展了AppCompatActivity {

long timeInMilliseconds = 0L;
long timeSwapBuff = 22320000; // 6Hours:12Minutes:00Seconds
long updatedTime = 0L;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
private Runnable updateTimerThread = new Runnable() {

    public void run() {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;
        String hms = String.format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(updatedTime),
                TimeUnit.MILLISECONDS.toMinutes(updatedTime) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(updatedTime)),
                TimeUnit.MILLISECONDS.toSeconds(updatedTime) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(updatedTime)));
        timerValue.setText(hms);
        customHandler.postDelayed(this, 0);
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timerValue = findViewById(R.id.timer);
    startButton = findViewById(R.id.start);
    pauseButton = findViewById(R.id.pause);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });

}

}

答案 1 :(得分:1)

我猜问题出在暂停按钮监听器上... 应该是

 pauseButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        timeSwapBuff = timeInMilliseconds;
        customHandler.removeCallbacks(updateTimerThread);

    }
});