java Java如何每秒增加一个int 1直到它达到15
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16748438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java how to increment an int 1 every second until it gets to 15
提问by James Young
I'd like to know how i can add 1 to my int every second until it reaches a certain number in this case 15.
我想知道如何每秒将 1 添加到我的 int 中,直到在这种情况下达到某个数字 15。
But i would only want the int to start increasing once i have pressed a button.
但是我只希望在按下按钮后 int 开始增加。
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
login.addAction(Actions.moveTo(0, 310, 1));
loginClicked = true;
if(loginClicked == true && loginTimer == 15){
login.addAction(Actions.moveTo(0, 430, 1));
}
}
});
There is my code as you can see i am making something move then after 15 seconds if it remains untouched i want it too move back.
有我的代码,正如你所看到的,我正在移动一些东西,然后在 15 秒后,如果它保持不变,我希望它也移动回来。
回答by zennon
You can use a Timer:
您可以使用计时器:
int delay = 5000; // delay for 5 sec.
int period = 1000; // repeat every sec.
int count = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
// Your code
count++;
}
}, delay, period);
回答by Raghunandan
Timer _t,timer;
int _count=1;
TextView _tv
Initialize the timer
初始化定时器
_tv = (TextView) findViewById( R.id.textView1 );
_t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
@Override
public void run() {
_count++;
runOnUiThread(new Runnable() //run on ui threa
{
public void run()
{
if(_count<=15)
{
_tv.setText(""+_count);
}
else
{
_t.cancel();
}
}
});
}
}, 1000, 1000 );
You can use a timer as above. but i would suggest you use to use a handler
您可以使用上面的计时器。但我建议你使用处理程序
Using Handler
使用处理程序
TextView _tv ;
Handler m_handler;//= new Handler();
Runnable m_handlerTask ;
_tv = (TextView) findViewById( R.id.textView1 );
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
if(_count<=15)
{
_tv.setText(""+_count);
_count++;
}
else
{
m_handler.removeCallbacks(m_handlerTask);
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
回答by Blackbelt
with an handler:
带有处理程序:
Handler handler = new Handler();
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (++counter < 15) {
handler.postDelayed(this, 1000L);
return;
}
handler.removeCallbacks(this);
}
}, 1000L);
}
回答by Danilo Deguia Leuterio
You can also use a Countdown Timer
just put at the ontick
an element of int = bob++;
您也可以使用Countdown Timer
just put 在ontick
an 元素上int = bob++;
String convertbob = String.valueof(bob);
TextViewName.setText(convertbob);
and what ever you wanted to do at onFinish
你想做什么 onFinish
I hope it would help others, its okay to cheat the real function of countdown just be wise.
希望能帮到别人,骗倒倒计时的真正功能没关系,聪明点就好了。