我在Android中使用计时器功能,我有1到up计时器的代码。请帮我使它成为一个倒计时计时器而不需要点击一个按钮?
public class MainActivity extends Activity {
public int time = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv = (TextView) findViewById(R.id.main_timer_text);
tv.setText(String.valueOf(time));
time += 1;
}
});
}
}, 0, 1000);
}
发布于 2013-12-15 01:59:14
您可以使用CountDownTimer来完成此操作
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilEnd) {
mTextView.setText(String.valueOf(millisUntilEnd / 1000));
}
public void onFinish() {
mTextView.setText("done");
}
}.start();
这将在20秒内每秒更新您的time TextView。
https://stackoverflow.com/questions/20586398
复制相似问题