简体   繁体   English

我想在textView中显示线程的计时器(实时)

[英]I would like to show the timer of a thread in textView (Live)

I have a timer in a thread waiting like 20 seconds before moving to a new activity, what I'm looking for is to show the time whatever decreasing or increasing in a textView in that while. 我在一个线程中有一个计时器,等待大约20秒后再转到新活动,我要寻找的是显示时间,无论那段时间内textView减少还是增加。 here's my code: 这是我的代码:

Thread timer = new Thread() {
        public void run() {
            try {
                sleep(ie);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {

                Intent i = new Intent(Activity.this, Menu.class);
                if (ie == 20000) {
                    startActivity(i);
                    overridePendingTransition(R.anim.pushin, R.anim.pushout);
                }
            }
        }
    };

    timer.start();

thanks for your assisstance 谢谢你的协助

Try a CountDownTimer instead of a Thread: 尝试使用CountDownTimer而不是线程:

            CountDownTimer count = new CountDownTimer(20000, 1000)
            {

                int counter = 20;

                @Override
                public void onTick(long millisUntilFinished)
                {
                    // TODO Auto-generated method stub
                    counter--;
                    textView.setText(String.valueOf(counter));

                }

                @Override
                public void onFinish()
                {

            startActivity(i);
            overridePendingTransition(R.anim.pushin, R.anim.pushout);
                }
            };

            count.start();

Camilo Sacanamboy's answer is the correct one. Camilo Sacanamboy的答案是正确的。 If you want to do this with a Thread, one solution might be like this: 如果要使用线程执行此操作,则一种解决方案可能是这样的:

final TextView status; //Get your textView here

    Thread timer = new Thread() {
        public void run() {
            int time = 20000;
            try {
                while(time > 0){
                    time -= 200; //Or whatever you might like

                    final int currentTime = time;
                    getActivity().runOnUiThread(new Runnable() { //Don't update Views on the Main Thread
                        @Override
                        public void run() {
                            status.setText("Time remaining: " + currentTime / 1000 + " seconds");
                        }
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {

                Intent i = new Intent(Activity.this, Menu.class);
                if (ie == 20000) {
                    startActivity(i);
                    overridePendingTransition(R.anim.pushin, R.anim.pushout);
                }
            }
        }
    };

    timer.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android Studio 中将实时 DateTime 显示为 textView(如数字手表)? - How to show the live DateTime as a textView (Like a digital watch) in Android Studio? 我有一个视图,并且在该视图内有一个textview我想更新该textview? - I have a view and inside that view I have a textview I would like to update that textview? 我想将图像保存为 sqlite 的路径并检索它然后在 ImageView 上显示 - I would like to save Image as path to sqlite and retrieve it then show on ImageView Android Studio-我想在Dialogfragment中按按钮显示日期选择器 - Android Studio - I would like to show an datepicker by button in Dialogfragment 我想获取所有联系人列表(内容提供商)并将其附加到TextView中 - I would like to get all the contact list(content provider) and append them into a TextView ScheduledExecutorService不会像Timer那样结束线程 - ScheduledExecutorService does not end thread like Timer 我无法在TextView中显示计时器 - I can't display the timer in a TextView 如何在共享首选项中保存textView? - How would I save a textView in Shared Preferences? 我该如何停止计时器? - How would i stop my timer? 当我在Java中单击它们时,我想显示相同的类图块 - I would like to show the same class tiles when i click on them in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM