单击按钮启动计时器 [英] Start timer on button click

查看:351
本文介绍了单击按钮启动计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,但我知道Java. 我的问题是,计时器在Android中如何工作?我读过,最好使用处理程序. 我要做的是,单击一个按钮,计时器开始计时.到单击按钮的那一刻,一切对我来说都是清楚的,但是如何启动计时器?

I'm new in Android programming but i know Java. My question is, how does a timer work in Android? I've read that is better to use a handler. What I want to do is, you click a button and the timer starts. To the moment when the button is clicked all is clear for me but how to start the timer?

推荐答案

计时器在Android中如何工作?

How does a timer work in Android?

您最好阅读计时器文档

到目前为止,当单击按钮时,一切都为我清除了;但是,如何启动计时器?

To the moment, when the button is clicked, all is cleared for me; but, how can I start the timer?

如果我没有误解您的问题,那么当您说Timer时,您指的是CounteDownTimer.因此,您应该具有以下内容:

If I didn't misunderstand your question, when you say Timer, you refer to CounteDownTimer. So, you should have something like this:

(我已经编写了示例代码.因此,您应该首先理解它,然后再将其应用于代码.)

(I've written a sample code. So, you should understand it first, and then, you should apply it to your code.)

添加Buttons

Adding the Buttons

btn1 = (Button)findViewById(R.id.bt1);
btn2 = (Button)findViewById(R.id.bt2);

添加SetOnClickListener()

Adding the SetOnClickListener()

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

btn2.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

我的btn1启动CountDownTimer,第二个停止并清除它.

My btn1 starts the CountDownTimer, and the second one stops and clears it.

现在,我用内部类 >名称.

public class CountDownTimerTest extends CountDownTimer {
    public CountDownTimerTest(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        text.setText("Time's up!");
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
    }

    @Override
    public void onTick(long millisUntilFinished) {
        text.setText("Time remain:" + millisUntilFinished);
        timeElapsed = startTime - millisUntilFinished;
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
    }
}

然后在我的btn1上,放置以下代码(启动CountDownTimer):

Then on my btn1, I put this code (start the CountDownTimer):

countDownTimer.start();

然后在我的btn2上,输入以下代码(停止/取消CountDownTimer):

And on my btn2, I put this code (stop/cancel the CountDownTimer):

countDownTimer.cancel();

现在,我希望您能理解CountDownTimer的工作原理,如果您的问题不是关于CountDownTimer的问题,请告诉我,我将尽快根据您的意愿更新答案.

Now, I hope that you can understand how CountDownTimer works, if your question isn't about CountDownTimer, let me know, and I'll update my answer as soon as possible with your wishes.

要使用相同的Button来执行此操作,可以执行以下操作:

To do it with the same Button, you can do this:

Boolean变量创建为:

Boolean ButtonClicked = false;

然后,按如下所示修改代码:

And then, modify the code as follows:

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if (!ButtonClicked)) {
            ButtonClicked = true;
            countDownTimer.start(); 
        } else {
            ButtonClicked = false;
            countDownTimer.cancel();
        }                       
    });
}

编辑2获取单击的按钮

您可以像这样创建名为NumberButtonClickedint:

int NumberButtonClicked = 0;

然后在每一个Button上都必须执行此操作(示例):

Then on every Button you have you'll have to do this (Example) :

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {
            NumberButtonClicked = 1;
    });
}

那么您知道,如果单击btn1,则变量将为1.

Then you know that if you have clicked btn1 your variable will be 1.

这篇关于单击按钮启动计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆