1. 程式人生 > >Android 更新UI的幾種方式

Android 更新UI的幾種方式

package lib.com.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView ;

    Handler handler = new Handler( ) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            textView.setText( "Ui更新了");
        }
    };

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

        textView = (TextView) findViewById( R.id.tv );
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep( 2000 );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                handler.sendEmptyMessage( 2 ) ;
            }
        }).start();

    }
}