美文网首页
SimpleHanderDemo

SimpleHanderDemo

作者: 张知卓见 | 来源:发表于2017-06-10 12:54 被阅读6次

    Demo

    GIF.gif

    source code

    https://github.com/iljyya/SimpleHanderDemo

    key code

    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        public TextView show;
        public Handler showhandler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            show = (TextView) findViewById(R.id.show);
            StringBuffer text = new StringBuffer();
            text.append("text view: wait 3s");
            show.setText(text);
            showhandler = new Handler();
            new Thread_1().start();
            new Thread_2().start();
        }
    
        class Thread_1 extends Thread {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                showhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        String line = "\n";
                        StringBuffer text = new StringBuffer(show.getText());
                        text.append(line).append("handler msg 2s thread 1");
                        show.setText(text);
                    }
                });
            }
        }
    
        class Thread_2 extends Thread {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                showhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        String line = "\n";
                        StringBuffer text = new StringBuffer(show.getText());
                        text.append(line).append("handler msg 3s thread 2");
                        show.setText(text);
                    }
                });
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:SimpleHanderDemo

          本文链接:https://www.haomeiwen.com/subject/npwqqxtx.html