【问题标题】:Updating TextView every N seconds?每N秒更新一次TextView?
【发布时间】:2011-01-23 21:06:48
【问题描述】:

我最近对此非常困惑,无法在任何地方找到答案。

在为 android 编程时,我想每 10 秒更新一次 textview,但我该怎么做呢?我已经看到一些示例使用“Run()”和“Update()”,但是当我尝试时这似乎没有帮助,有什么想法吗?

现在我有:

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.slideshow); CONST_TIME = (int) System.currentTimeMillis(); Resources res = getResources(); myString = res.getStringArray(R.array.myArray); } public void checkTime(View V){ TextView text = (TextView) findViewById(R.id.fadequote); CUR_TIME = (int) System.currentTimeMillis(); text.setText(""+(int) (CUR_TIME-CONST_TIME));//Debugs how much time has gone by if(CUR_TIME-CONST_TIME>10000){ getNextQuote(null); //A function that gets a random quote CONST_TIME = CUR_TIME; } }

我想我真正要问的是如何让 checkTime() 无休止地重复它,直到 onPause() 被调用?

【问题讨论】:

    标签: android textview self-updating


    【解决方案1】:

    不要大惊小怪地使用后台线程然后runOnUiThread(),而是使用postDelayed(),在任何View 上都可用,来安排RunnableRunnable 可以更新您的 TextView,然后安排自己进行下一次传递。使用后台线程来查看时间流逝是一种浪费。

    【讨论】:

    • 在更新列表视图项时如何使用此机制?鉴于可能会重用视图,在这种情况下,需要删除先前计划的可运行对象。如何删除已发布的可运行文件?
    • @500865: "你如何删除已发布的可运行文件" -- 致电 removeCallbacks()
    • 感谢@CommonsWare。我从未在 View 类中注意到该方法。那应该可以。
    • 我知道这是一篇旧帖子,但我不得不点赞并发表评论,因为这是一个简单的天才。我有回收站视图,其项目有点复杂,它还有一个显示滴答时间的文本视图。现在要更新每个文本视图,您不能简单地调用任何通知方法。通过这种方式,只有文本视图被更新。出色的@CommonsWare 谢谢
    • @SarthakMittal:当您不再需要定期工作时,请致电removeCallbacks(),或致电onDestroy()。在那之前,什么都不会泄露,因为View 和它的Activity 仍然存在。
    【解决方案2】:

    使用计时器怎么样?

    private Timer timer = new Timer();
    private TimerTask timerTask;
    timerTask = new TimerTask() {
     @Override
     public void run() {
        //refresh your textview
     }
    };
    timer.schedule(timerTask, 0, 10000);
    

    通过 timer.cancel() 取消它。在你的 run() 方法中,你可以使用 runOnUiThread();

    更新:

    我有一个实时评分应用程序,它使用此计时器每 30 秒更新一次。它看起来像这样:

    private Timer timer;
    private TimerTask timerTask;
    
    public void onPause(){
        super.onPause();
        timer.cancel();
    }
    
    public void onResume(){
        super.onResume();
        try {
           timer = new Timer();
           timerTask = new TimerTask() {
              @Override
              public void run() {
             //Download file here and refresh
              }
           };
        timer.schedule(timerTask, 30000, 30000);
        } catch (IllegalStateException e){
           android.util.Log.i("Damn", "resume error");
        }
    }
    

    【讨论】:

    • 我在哪里放“Timertask = new TimerTask(){...}; timer.schedule(tiemrTask,0,10000);" ?就在我的班级下还是我需要把它放在 onResume(); 下?因为如果我把它放在我的课堂上会出错。
    • 所以,我确实做到了(除了“//在此处下载文件并刷新”之外,我做了“getNextQuote(null)”,它将随机从数组中找到一个引用并使用新报价,但它只会执行一次,而不是连续执行。如果这会产生任何差异,我将 Timertask 设置为 10。LogCat 中也没有错误
    • 其实我弄错了,我把"android.util.Log.i("Success","getNextQuote returned");"在 getNextQuote(null) 之后;它每 10 秒出现一次!.. 所以很可能是函数本身的错误.. 谢谢!
    • 不客气。我很高兴能帮助你。要更新 TextView,您可以使用 runOnUiThread 之类的东西,如果您预计会出现以下错误:“只有创建视图层次结构的原始线程才能触及其视图”。祝你好运!
    【解决方案3】:

    我同意 Wired00 的回答,但请遵循以下顺序:

            //update current time view after every 1 seconds
            final Handler handler=new Handler();
    
            final Runnable updateTask=new Runnable() {
                @Override
                public void run() {
                    updateCurrentTime();
                    handler.postDelayed(this,1000);
                }
            };
    
            handler.postDelayed(updateTask,1000);
    

    【讨论】:

    • 那个帖子延迟多次调用 1000 是什么意思?为什么不只是 1 postDelayed 只?如果我没记错 Handler 现在已被弃用,对吗?还有其他选择吗?
    【解决方案4】:

    如果它可以帮助某人这里是使用postDelayed()的示例代码

    ...

    private Handler mHandler = new Handler();
    

    ...

    // call updateTask after 10seconds
    mHandler.postDelayed(updateTask, 10000);
    

    ...

    private Runnable updateTask = new Runnable () {
        public void run() {
            Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
                    "updateTask run!");
    
                        // run any code here...         
    
                        // queue the task to run again in 15 seconds...
                        mHandler.postDelayed(updateTask, 15000);
    
    
        }
    };
    

    【讨论】:

      【解决方案5】:

      使用线程。见Painless Threading

      【讨论】:

      • 单击链接会导致 404:未找到
      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2013-01-26
      • 2017-05-09
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多