本帖最后由 bigapple88 于 2011-03-08 12:23:37 编辑

解决方案 »

  1.   

    第一种方法是启动的是新线程,所以UI线程继续运行,Button可以显示,但是第二种方法并没有新线程,所以消息的处理阻塞了UI的渲染过程。
      

  2.   

    第二种方法没有启动新线程吗?那怎么我在run()方法中打印出来的线程ID和主线程ID不同啊,应该是启动了新的线程吧?
      

  3.   

    哦,不好意思,刚才打印的地方确实说明第二种方法没有启动新的线程,但是为什么myHandler.post(myThread);没有启动新的线程呢
      

  4.   

    API里面说,post方法的Runnable使用Handler所在的线程执行。Handler又在主线程中,所以Runnable执行时主线程会被Pending。就是你说的那个button显示不出来的原因
      

  5.   

    第二中方法没有启动新线程,启动线程必须执行start函数,而runnable接口没有start方法,Thread才有start方法。
    你还需要执行new Thread(myThread ).start();来启动新线程,Thread有个参数为runnable对象的构造函数。
    我也是前几天才明白这个问题。
      

  6.   

    第二种方法是通过handler往主线程的消息队列添加消息,当然还是在主线程执行回调函数,阻塞了主线程
      

  7.   

    public final boolean post (Runnable r) 
    Since: API Level 1 Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.
    谢谢楼上的各位,post (Runnable r)中Runnable对象确实是在创建handler的线程中(本例是主线程)执行。