我想创建一个可以在2秒内设置变量的应用
final Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int decis = (int) ((millis % 1000)/10);
TimerBox.setText(String.format("Time: %d.%02d", seconds, decis));
Tx.setText(""+counting);
timerHandler.postDelayed(this, 10);
if(seconds == 2){
counting = 1;
seconds = 0;
decis = 0;
}
}
};
这不是我的代码,在android studio中是非常新的
答案 0 :(得分:-1)
new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
time = System.currentTimeMillis();
if(time>=2000)
{
time=0;
//do things here
}
}
}
}).start();
通过这种方式,您可以知道何时通过两秒