view.post使用说明

在使用view post的时候,可以直接在非UI线程中更新UI控件,在onclick中创建一个线程

  new Thread(new Runnable() {
            int i;
            @Override
            public void run() {
                //mTextView.setText("A change in text, thank you very much");
                while (true){
                    i++;
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //System.out.println(Thread.currentThread().getName());

                    //mTextView.setText(String.valueOf(i));
                    mTextView.post(new Runnable(){
                        @Override
                        public void run() {
                            mTextView.setText(String.valueOf(i));
                        }
                    });
                }

            }
        }).start();

java接口说明,java接口中可以声明为final的变量,声明为public static final的类型,但是不能声明有变量,可以声明常量,就是这个说法;接口只能这样用

每一秒更新一下空间上的数值,如果直接在线程中显示setText的数值,程序会报错

通过vew。post的函数,将Runnable加入到ui线程的消息队列,直接更新ui中的数据,不需要使用handle进行消息的传递和处理

 

posted on 2017-09-30 11:31  tistar  阅读(827)  评论(0)    收藏  举报