与处理程序,并postDelayed android中暂停 [英] Pausing with handler and postDelayed in android

查看:99
本文介绍了与处理程序,并postDelayed android中暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新至Android编程所以请原谅我的noobie的烦躁。我试图创建一个将在布局的中间的一个TextView的,只是有切换到不同的文本每两秒钟很简单的活动。例如,TextView的会说text1中,停顿了几秒钟,然后说文本2,然后再次暂停。最后,我要添加更多的文本,并让它们全部循环一个接一个。我知道这似乎是一个超级简单的事情,但我主要是想了解线程和处理程序,在这个时刻。不管怎么说,我已经读了我们应该如何保持长时间的事情了UI线程以prevent一个错误,所以我想我会使用一个处理程序简单地在屏幕上2文本之间切换不幸的是,我不能得到这个工作,下面是一些code:

I'm very new to android programming so please forgive my noobie-ness. I'm trying to create a very simple activity that will have one TextView in the middle of the Layout and just have it switch to a different text every couple of seconds. For example, the TextView will say "text1", pause for a couple of seconds, then say "text2, and pause again. Eventually, I want to add more texts and have them all cycle one after another. I know this seems like a super simple thing but I'm mainly trying to learn about threads and handlers at this moment. Anyways, I've read up on how we should keep lengthy things off the UI thread to prevent an error so I thought I'd use a handler to simply switch between 2 texts on screen. Unfortunately, I can't get this to work. Here's some code:

public class MainActivity extends Activity {

String[] myarray = {"text1" , "text2"};
int arraylength = myarray.length;
int count;
Handler handler = new Handler();

TextView mytexts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mytexts = (TextView)findViewById(R.id.my_texts);
    mytexts.setText(myarray[0]);

    Thread t = new Thread( new Runnable(){
        public void run() {
            for (int count = 0; count < arraylength; count++){
                handler.postDelayed(new Runnable(){
                    public void run() {
                        mytexts.setText(myarray[1]);
                    }                   
                }, 7000);
            }
        }
    });
    t.start();
    }
}

从我可以在logcat中看到的,处理程序似乎运行陆续postDelayed一个正确的(在我的code的情况下,不等待7秒与postDelay另做postDelayed)。另外,我想使1mytexts.setText(myArray的[1]);是相同的,在计数for循环,因此它可以是相同的阵列中的字符串之一但给我一个错误。我一直停留在这几个小时,我在网上找到其他的例子似乎太复杂,对于像我这样谁主要是希望的基本下跌之前,我可以处理其他事情。任何帮助都与任何这将是多大的AP preciated。谢谢你。

From what I can see in the logcat, the handler seems to run postDelayed one right after another (in my code's case, it does NOT wait 7 seconds with the postDelay to do another postDelayed). Also, I would like to make the 1 in "mytexts.setText(myarray[1]);" be the same as "count" in the for loop so it can be the same as one of the strings in the array but that gives me an error. I've been stuck on this for hours and other examples I've found online seem way too complicated for someone like me who mainly wants to get the basics down before I can tackle other things. Any help at all with any of this would be much appreciated. Thank you.

推荐答案

postDelayed 是非阻塞,这意味着它会添加到队列中的我以后会做。那么,你很可能看到的是所有的文字更新一起发生在第7秒。我这样说是因为你是 postDelay ING从的onCreate 方法时,在现实中,你可能想从<$做到这一点C $ C> onResume 甚至 onPostResume

postDelayed is non blocking, meaning it would add it to a queue of I'll do this later. So what you are probably seeing is all text updates happening together at the 7th second. I say this because you are postDelaying from the onCreate method when in reality you probably want to do it from onResume or even onPostResume.

也没有理由来创建一个线程可运行添加到后队列。您的code应该看起来更像是这样的: (注意时间延迟乘数)

Also there is no reason to create a thread to add runnables to the post queue. Your code should look more like this: (Note the time to delay multiplier)

@Override
protected void onResume() {
    super.onResume();
    for (int count = 0; count < arraylength; count++){
        handler.postDelayed(new Runnable(){
            @Override
            public void run() {
                mytexts.setText(myarray[count]);
            }
        }, 7000 * (count + 1));
    }
}

这篇关于与处理程序,并postDelayed android中暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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