开发者

I want show the elapsed seconds from 60 to 1

开发者 https://www.devze.com 2023-03-15 17:14 出处:网络
I want to show a text view with elapsed seconds from 60 to 1开发者_Go百科. How should I take handler event?

I want to show a text view with elapsed seconds from 60 to 1开发者_Go百科.

How should I take handler event?

time = GetTime.Showtime();
elapsetime.setText(time + " Secs");


Use a CountDownTimer, http://developer.android.com/reference/android/os/CountDownTimer.html


First of all, you have to create and initialize Timer object:

Timer myTimer;

myTimer = new Timer();

After that you can call use the schedule method to call timerMethod() (or your method). It will the timerMethod() every second (1000 milliseconds).
myTimer.schedule(new TimerTask() {
@Override
public void run() {
timerMethod();
}
}, 0, 1000);

//Runs your doSomething() in the UI Thread

private void timerMethod()
{
this.runOnUiThread(doSomething);
}

// make your doSomething()  runnable

private Runnable doSomething = new Runnable() {
public void run() {
// Your code for doing something
}


i use handler thread runnable.

handler =new Handler();
         runnable = new Runnable() {
            @Override
            public void run() {

                elapsetime.setText(time+" Secs");
                time--;
                if(time<1){
                    handler.removeCallbacks(runnable);
                }else{
                handler.postDelayed(this, 1000);
                }
            }

        };

        handler.postDelayed(runnable, 1000);
0

精彩评论

暂无评论...
验证码 换一张
取 消