Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使计时器在单击时停止_Java_Android_Timer - Fatal编程技术网

Java 如何使计时器在单击时停止

Java 如何使计时器在单击时停止,java,android,timer,Java,Android,Timer,我的计时器到达某个数字时就会停止。相反,我希望它停止在一个按钮点击。我该怎么做? 这就是我的代码当前的样子: final TextView t1 = (TextView) findViewById(R.id.yourpay); final Timer t =new Timer(); t.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new R

我的计时器到达某个数字时就会停止。相反,我希望它停止在一个按钮点击。我该怎么做? 这就是我的代码当前的样子:

final TextView t1 = (TextView) findViewById(R.id.yourpay);

final Timer t =new Timer();
t.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                    public void run() {
                        money = (PPS+Reserve);
                        Reserve = (money);
                        t1.setText("$" + money); //Place your text data here
                        counter++;

                        //Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop. 
                        if(counter == HPDPS)
                            t.cancel(); 
                    }
            }); 
        }
    }, 1000, 1000);

如果可能的话,我希望它在点击按钮时停止,并在计数器达到HPDPS时停止。

使用倒计时。单击按钮时,只需停止计时器


但对于您刚刚创建的按钮,请在按钮上设置OnClickListener,然后在侦听器的onClick()方法上调用timer.cancel()或任何停止它的命令。

放入按钮的
OnClickListener()

并从计时器中删除停止条件


代码示例(更新):

final TextView t1 = (TextView) findViewById(R.id.yourpay);

final Timer t =new Timer();
t.schedule(new TimerTask() {

    @Override
    public void run() {
        runOnUiThread(new Runnable() {

            public void run() {
                money = (PPS+Reserve);
                Reserve = (money);
                t1.setText("$" + money); //Place your text data here

                // Removed the stopping condition/counter

            }
        }); 
    }
}, 1000, 1000); // Do you really want to wait 1 second before executing the timer's code?  If not, change the 1st "1000" to a "0"


final Button b = (Button) findViewById(R.id.my_button_id); // Replace with your button's id
b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (t != null)
            t.cancel();
        }
});

你能给我举个例子吗?就像我说的,我对这个很陌生,所以我真的不知道如何格式化onClickListener谢谢你的帮助!@override有几个错误,所以我必须删除它?我不知道为什么
final TextView t1 = (TextView) findViewById(R.id.yourpay);

final Timer t =new Timer();
t.schedule(new TimerTask() {

    @Override
    public void run() {
        runOnUiThread(new Runnable() {

            public void run() {
                money = (PPS+Reserve);
                Reserve = (money);
                t1.setText("$" + money); //Place your text data here

                // Removed the stopping condition/counter

            }
        }); 
    }
}, 1000, 1000); // Do you really want to wait 1 second before executing the timer's code?  If not, change the 1st "1000" to a "0"


final Button b = (Button) findViewById(R.id.my_button_id); // Replace with your button's id
b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (t != null)
            t.cancel();
        }
});