本文介绍了每5秒用String-array中的每个字符串更新TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView ,我想每5秒用我的String中的每个字符串更新 TextView 数组。

I have a TextView and I would like to update the TextView every 5 second with each string in my String Array.

这是我尝试的代码。

TextView display;
EditText caption;
Thread thread;
String blinks;
String[] wc;
private CountDownTimer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    display = (TextView) findViewById(R.id.display);
    caption = (EditText) findViewById(R.id.caption);

    timer = new CountDownTimer(5000, 20) {

        @Override
        public void onTick(long millisUntilFinished) {

            String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
            for (int j = 0; j < wc.length; j++) {

                blinks = wc[j];
                final String[] titles = {"" + blinks + ""};

                for (int i = 0; i < titles.length; i++) {
                    display.setText(titles[i]);
                }

            }


        }

        @Override
        public void onFinish() {
            try{
                yourMethod();
            }catch(Exception e){

            }
        }
    }.start();
}


推荐答案

final String[] wc = {"The", "Qucik", "Brown", "fox", "Jumped"};
        final android.os.Handler handler = new android.os.Handler();
        handler.post(new Runnable() {

            int i = 0;

            @Override
            public void run() {
                display.setText(wc[i]);
                i++;
                if (i == wc.length) {
                    handler.removeCallbacks(this);
                } else {
                    //5 sec
                    handler.postDelayed(this, 1000 * 5);
                }
            }
        });

这篇关于每5秒用String-array中的每个字符串更新TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:38