如何在一个无限循环中不断更新活动中的TextView? [英] How to update a TextView in an activity constantly in an infinite loop?

查看:153
本文介绍了如何在一个无限循环中不断更新活动中的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有TextView的活动,我想不断更新文本.

I have an activity which has a TextView, and I want to update the text constantly..

在Java中,我可以做一个无限的while循环,并在每次迭代时都设置文本.

In Java I could just make an infinite while loop, and just set the text each iteration.

但是当我尝试在Android中执行此操作时,它显示黑屏,甚至没有加载活动.

But when I try to do that in Android, it shows a black screen, and doesn't even load the activity.

我将无穷大放在onCreate方法中,也许这就是它崩溃的原因..如果是,那么我应该把它放在哪里?

I put the infinite in the onCreate method, perhaps that's why it crashes..but if it is, where should I put it?

推荐答案

使用处理程序和一个单独的线程/可运行线程,用于不断更新TextView而不是While循环:

use Handler and a separate thread/runnable for updating TextView constantly instead of While loop :

Handler handler=new Handler();
handler.post(new Runnable(){ 
    @Override
    public void run() {
        // upadte textView here
        handler.postDelayed(this,500); // set time here to refresh textView
    }
});

这篇关于如何在一个无限循环中不断更新活动中的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆