编写一个将时间条件与动作结合的代码。

huangapple go评论71阅读模式
英文:

Coding a time condition together with an action

问题

当应用程序启动时,我有一个按钮(btn1)和一个启动计时器(long start = System.nanoTime();)。

现在,有两种情况可能发生:

  • 要么在 1 分钟的时间限制之前的任何时候点击按钮(执行一些代码_A)
  • 要么计时器(long elapsedTime = System.nanoTime() - start;)达到了 1 分钟的时间限制(执行一些代码_B)

我已经尝试了很多不同的组合,但在涉及时间维度和表达/触发时间条件或操作条件(按钮 btn1 被按下)的 if/while 或其他循环方面我陷入了困境。

是否有人有主意?那将会非常有帮助。

英文:

When the app launch, I have a button (btn1) and a timer starting (long start = System.nanoTome();).

Now, two things can happen:

  • either the button is clicked at any time BEFORE the time limit of 1 minute (do some code_A then)
  • or the timer (long elapsedTime = System.nanoTome() - start;) reaches the time limit of 1 minute (do some code_B then)

I have tried lots of various combinations, but I stuck on the logic with regard to the time dimension and the if/while or whatever loop that would express/trigger BOTH the time condition OR the action condition (button btn1 pressed) in parallel.

Does anybody have an idea? That would be of great help.

答案1

得分: 2

你可以使用 CountDownTimer 并在其中处理状态,然后根据状态调用适当的方法:

Kotlin 版本:

class MyCountdownTimer : CountDownTimer(60000, 1000) {
    
    private var state: CountdownState = CountdownState.IDLE
    
    override fun onFinish() {
        state = CountdownState.FINISHED
    }

    override fun onTick(p0: Long) {
        if(state == CountdownState.IDLE) {
            state = CountdownState.COUNTING
        }
    }

    fun getState(): CountdownState {
        return state
    }
}

enum class CountdownState {
    IDLE, COUNTING, FINISHED
}

在你的代码中:


val countdownTimer = MyCountdownTimer()
btn1.setOnClickListener {
    countdownTimer.start()
}

btn2.setOnClickListener {
    if(countdownTimer.getState() == CountdownState.COUNTING) {
        callMethodWhenRunning()
    } else if (countdownTimer.getState() == CountdownState.FINISHED) {
        callMethodWhenFinished()
   }
}

Java 版本:

import android.os.CountDownTimer;

public class MyCountdownTimer extends CountDownTimer {
    
    private CountdownState state = CountdownState.IDLE;
    
    public MyCountdownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long l) {
        if(state == CountdownState.IDLE) {
            state = CountdownState.COUNTING;
        }

    }

    @Override
    public void onFinish() {
        state = CountdownState.FINISHED;
    }
    
    public CountdownState getState() {
        return state;
    }
}

enum CountdownState {
    IDLE, COUNTING, FINISHED
}

使用方法:

MyCountdownTimer timer = new MyCountdownTimer(60000, 1000);
btn1.setOnClickListener(view -> {
    timer.start();
});
btn2.setOnClickListener(view -> {
    if(timer.getState() == CountdownState.COUNTING) {
        callMethodWhenRunning();
    } else if (timer.getState() == CountdownState.FINISHED) {
        callMethodWhenFinished();
   }
});
英文:

You can use CountDownTimer and handle the state there, then invoke the proper method depending on the state:

Kotlin version:

class MyCountdownTimer: CountDownTimer(60000, 1000) {
    
    private var state: CountdownState = CountdownState.IDLE
    
    override fun onFinish() {
        state = CountdownState.FINISHED
    }

    override fun onTick(p0: Long) {
        if(state == CountdownState.IDLE) {
            state = CountdownState.COUNTING
        }
    }

    fun getState(): CountdownState {
        return state
    }
}

enum class CountdownState {
    IDLE, COUNTING, FINISHED
}

And in your code:


val countdownTimer = MyCountdownTimer()
btn1.setOnClickListener {
    countdownTimer.start()
}

btn2.setOnClickListener {
    if(countdownTimer.getState() == State.RUNNING) {
        callMethodWhenRunning()
    } else if (countdownTimer.getState == State.FINISHED) {
        callMethodWhenFinished()
   }
}

Java version:

import android.os.CountDownTimer;

public class MyCountdownTimer extends CountDownTimer {
    
    private CountdownState state = CountdownState.IDLE;
    
    public MyCountdownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long l) {
        if(state == CountdownState.IDLE) {
            state = CountdownState.COUNTING;
        }

    }

    @Override
    public void onFinish() {
        state = CountdownState.FINISHED;
    }
    
    public CountdownState getState() {
        return state;
    }
}

enum  CountdownState {
    IDLE, COUNTING, FINISHED
}

and usage:

MyCountdownTimer timer = new MyCountdownTimer(60000, 1000);
btn1.setOnClickListener(view -> {
    timer.start();
});
btn2.setOnClickListener(view -> {
    if(timer.getState() == State.RUNNING) {
        callMethodWhenRunning();
    } else if (timer.getState == State.FINISHED) {
        callMethodWhenFinished();
   }
});

答案2

得分: 2

你可以使用CountDownTimer:https://developer.android.com/reference/android/os/CountDownTimer

        CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {
                codeB();
            }
        }.start();
        Button button = new Button(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.cancel();
                codeA();
            }
        });

这将创建一个时长为10秒的计时器,onTick 方法每隔1秒被调用一次。
new CountDownTimer(timerLength, tickInterval)

为了在评论中进一步可视化您的问题,无论计时器是否停止:

编写一个将时间条件与动作结合的代码。

在这张截图中,我让计时器运行完毕。计时器设置为5秒,5秒后调用了 onFinish() 方法,如消息所示。

编写一个将时间条件与动作结合的代码。

在这张截图中,我通过 onClickListener 取消了计时器,可以看到中间没有消息,因此 onFinish() 没有被调用。

英文:

You can use the CountDownTimer: https://developer.android.com/reference/android/os/CountDownTimer

        CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {
                codeB();
            }
        }.start();
        Button button = new Button(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.cancel();
                codeA();
            }
        });

This will create a timer with the length of 10 seconds and the onTick method is called every 1 second.
new CountDownTimer(timerLength, tickInterval)

To further visualize your question in the comments, whether the timer stops or not:

编写一个将时间条件与动作结合的代码。

In this screenshot, I've let the timer run out. The timer was set to 5 seconds and after 5 seconds onFinish() was called, as seen by the message.

编写一个将时间条件与动作结合的代码。

In this screenshot, I've cancelled the timer through the onClickListener, as you can see there is no message in the center, thus onFinish() hasn't been called.

huangapple
  • 本文由 发表于 2020年10月2日 17:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定