我的计时器将在达到某个数字时停止。相反,我希望它在单击按钮时停止。我怎么做?这是我的代码目前的样子:
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 时停止。