在 Android 中允许按钮在一定时间内给予积分

发布于 2024-12-12 01:47:40 字数 354 浏览 0 评论 0原文

我只看到有关如何在一定时间后执行某些操作的文章,但我想知道如何在 Android 中执行特定时间的某些操作。

    public void buttonClick(View v) {

        currentUser.changeScore(10);

        TextView tv = (TextView) findViewById(R.id.score);
        tv.setText("Score: " + currentUser.getScore());

    }

我希望用户能够在事件发生后 10 秒内按下按钮并获得积分。在那 10 秒之后,如果按下按钮,我希望用户失去 10 分。

I am only seeing articles on how to do something after a certain amount of time, but I want to know how to do something FOR a certain amount of time in Android.

    public void buttonClick(View v) {

        currentUser.changeScore(10);

        TextView tv = (TextView) findViewById(R.id.score);
        tv.setText("Score: " + currentUser.getScore());

    }

I want to the user to be able to press the button and gain points for 10 seconds after an Event has occured. After those 10 seconds, I want the user to lose 10 points if the button is pressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2024-12-19 01:47:40

这很简单。每次事件发生时,设置一个变量,如下所示:

long positivePointEndTime = 0;

// In your event handler...
positivePointEndTime = System.currentTimeMillis() + 10000;

// Then in the button click handler
int scoreChange = System.currentTimeMillis() < positivePointEndTime ? 10 : -10;
currentUser.changeScore(scoreChange);

您并不是真的希望在该时间间隔内定期更改任何内容 - 事件只是设置一个新的时间点,如下所示何时“可以”单击按钮的边界。不需要计时器或类似的东西。

(为了可测试性,您可能需要依赖注入的 Clock 类型或类似的类型来提供“当前时间”功能,但基本原理是相同的。)

It's simple. Every time your event happens, set a variable, like this:

long positivePointEndTime = 0;

// In your event handler...
positivePointEndTime = System.currentTimeMillis() + 10000;

// Then in the button click handler
int scoreChange = System.currentTimeMillis() < positivePointEndTime ? 10 : -10;
currentUser.changeScore(scoreChange);

It's not like you're really wanting anything to change on a regular basis during that interval - the event is just setting a new point in time as the boundary of when it's "good" to click the button. No need for a timer or anything like that.

(For testability you may want to have a dependency-injected Clock type or something similar to provide the "current time" functionality, but the basic principle is the same.)

南风起 2024-12-19 01:47:40

您可以使用计时器:

timerIsOn = true; // timerIsOn is boolean variable, must be visible for button click handler
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        timerIsOn = false;
    }
};
Timer timer = new Timer();
timer.schedule(task, 10000);

然后按钮单击处理程序将是:

public void buttonClick(View v) {
    if (timerIsOn) {
        currentUser.changeScore(10);
    } else {
        // reduce score
    }
    TextView tv = (TextView) findViewById(R.id.score);
    tv.setText("Score: " + currentUser.getScore());
}

You may use Timer:

timerIsOn = true; // timerIsOn is boolean variable, must be visible for button click handler
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        timerIsOn = false;
    }
};
Timer timer = new Timer();
timer.schedule(task, 10000);

Then button click handler will be:

public void buttonClick(View v) {
    if (timerIsOn) {
        currentUser.changeScore(10);
    } else {
        // reduce score
    }
    TextView tv = (TextView) findViewById(R.id.score);
    tv.setText("Score: " + currentUser.getScore());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文