Thursday, July 5, 2012

Timer-倒數計時

倒數計時, 除了簡單之外...實在想不出其他的形容詞~
直接進入程式碼看看(下文程式參考於CountDownTimer)


public class TimerDemoActivity extends Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        tv = (TextView)findViewById(R.id.timer);
     
        // CountDownTimer(倒數計時總長,倒數計時間隔)
        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                tv.setText("seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                tv.setText("done!");
            }
         }.start();
    }
 
    // 用來呈現的TextView
    private TextView tv = null;
}