1

我是新手JavaAndroid我想在我的活动中设置一个ACTION_UP计时器,并在我做其他活动时取消计时器。我怎样才能基本上为此设置一个计时器并停止和重置其他事件的计时器?

4

3 回答 3

2

对于CountDownTimerHere I've started 30 seconds of time ticker,例如

CountDownTimer countDownTimer;
TextView tvTicker = (TextView) findViewById(R.id.tvTicker);

public void startClicked(View view) { //When button start is clicked

    countDownTimer = new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            tvTicker.setText("seconds remaining: " + millisUntilFinished / 1000);
//Do some stuff here for saving the duration to a variable or anything else as your requirements
        }

        public void onFinish() {
            tvTicker.setText("done!");
        }
    }.start();
}

方法说明

CountDownTimer(long millisInFuture, long countDownInterval)

millisInFuture=以毫秒为单位的时间

countDownInterval=以毫秒为单位的间隔

现在您可以使用这些方法进行其他类型的操作。

countDownTimer.cancel(); //Cancel the countdown.
countDownTimer.onFinish() //Callback fired when the time is up.
countDownTimer.onTick(long millisUntilFinished); //Callback fired on regular `interval. millisUntilFinished is The amount of time until finished.`
于 2015-12-15T05:03:08.433 回答
1

定时器开始时间。

在启动计时器单击事件中设置此项。

Date startDate = new Date();
long startTime = 0;
startTime = startDate.getTime();

将 startTime 存储在全局变量中,以便您以后可以使用该变量。

定时器结束时间。

在停止计时器单击事件中设置此项。

Date endDate = new Date();
long endTime = 0;
endTime = endDate.getTime();

现在以毫秒为单位获取时差

long timeDiff = endTime - startTime;
于 2015-12-15T05:04:27.203 回答
0

你必须试试这个

public boolean onTouchEvent(MotionEvent event) {
    boolean touch;
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                touch = false;
                break;
            case MotionEvent.ACTION_UP:
                touch = true;
                // Code for Timer
                break;
        }
    return true;
    }

您必须在此代码中编写处理程序并在其他事件上重置处理程序。处理程序代码

new Handler().postDelayed(new Runnable() {
        public void run() {
            //Your Task                   
        }
    }, TIME);
于 2015-12-15T05:00:45.313 回答