我想显示从 60 到 1 所经过的秒数

发布于 2024-11-17 09:32:02 字数 145 浏览 5 评论 0原文

我想显示一个文本视图,其经过的秒数从 60 到 1。

我应该如何获取处理程序事件?

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

I want to show a text view with elapsed seconds from 60 to 1.

How should I take handler event?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-11-24 09:32:02

首先,您必须创建并初始化 Timer 对象:

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
}

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
}
人心善变 2024-11-24 09:32:02

我使用可运行的处理程序线程。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文