1

私はJavaとを初めて使用します。イベントAndroidにタイム カウンターを設定しACTION_UP、他のイベントを実行している間はタイマーをキャンセルしたいと考えています。基本的にそのタイマーを設定し、他のイベントのタイマーを停止してリセットするにはどうすればよいですか?

4

3 に答える 3

2

Here CountDownTimerI've started 30 seconds of time ticker like

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 に答える