使用动态文本更新 UI [英] Update the UI with dynamic text

查看:26
本文介绍了使用动态文本更新 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每 5 秒更新一次屏幕上的文本,为此我创建了一个计时器.但是,在第一次更新后,它再也不会更新该框.我假设我需要刷新视图或其他东西,但我现在确定如何,有什么想法吗?

I wish to update the text on the screen every 5 second, I have created a timer to do so. However after the first update it never updates the box again. I am assuming I need to refresh the view or something but I am now sure how, any Ideas?

public class HomeActivity extends Activity implements OnClickListener {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textTitle = (TextView) findViewById(R.id.textTitle);
        textArtist = (TextView) findViewById(R.id.textArtist);
        timedMeta();
}
static void timedMeta()
{
    timer.scheduleAtFixedRate(   
        new TimerTask() {
            public void run() {
                try{      
                    textTitle.setText(title);
                    textArtist.setText(artist); 
                    }
                Thread.sleep(UPDATE_INTERVAL);
                catch (Exception e) 
                { 
                },
                DELAY_INTERVAL,
                UPDATE_INTERVAL);
           }
       }
   )
}

推荐答案

我会使用 Handler.

I would use a Handler.

private static final int WHAT = 1;
private static final int TIME_TO_WAIT = 5000;

Handler regularHandler = new Handler(new Handler.Callback() {
    public boolean handleMessage(Message msg) {
        // Do stuff

        regularHandler.sendEmptyMessageDelayed(msg.what, TIME_TO_WAIT);

        return true;
    }
});

regularHandler.sendEmptyMessageDelayed(WHAT, TIME_TO_WAIT);

例如,这将每 5000 毫秒做一些事情".您可以通过将 WHAT 作为不同的整数传入并在 handleMessage 函数中对其进行处理,从而使 Handler 对不同的事件做出反应.

As an example, that would "Do stuff" every 5000 milliseconds. You can make the Handler react to different events by passing in WHAT as a different integer and handling that in the handleMessage function.

我通常会将常量和处理程序作为成员放在类中,并将regularHandler.sendEmptyMessageDelayed(...) 放在 onResume() {}

I would normally place the constants and the Handler in the class as members and the regularHandler.sendEmptyMessageDelayed(...) in onResume() {}

我也会把它放在 onPause() {}

I would also put this in onPause() {}

regularHandler.removeMessages(WHAT)

Edit2:示例:

public class HomeActivity extends Activity implements OnClickListener {
    private static final int WHAT = 1;
    private static final int TIME_TO_WAIT = 5000;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textTitle = (TextView) findViewById(R.id.textTitle);
        textArtist = (TextView) findViewById(R.id.textArtist);
    }

    @Override
    public void onResume() {
        super.onResume();
        regularHandler.sendEmptyMessageDelayed(WHAT, TIME_TO_WAIT);
    }

    @Override
    public void onPause() {
        super.onPause();
        regularHandler.removeMessages(WHAT);
    }

    Handler regularHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message msg) {
            // Do stuff

            regularHandler.sendEmptyMessageDelayed(msg.what, TIME_TO_WAIT);

             return true;
        }
    });
}

您需要在 onResume() 和 onPause() 中执行此操作,因为如果您不将其放入 onPause 中,则当您的 Activity 不在前台时,处理程序将继续循环.您将希望循环在返回前台时再次启用(因此是 onResume()).

You need to do it in onResume() and onPause() because if you don't put it in onPause the Handler will continue to loop while your Activity isn't in the foreground. You will want the loop to enable again when it comes back to the foreground (hence onResume()).

这篇关于使用动态文本更新 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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