package com.pingyijinren.test; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity{ public static final int UPDATE_TEXT=1; private TextView textView; private Button button; private Handler handler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ case UPDATE_TEXT: textView.setText("HelloWorld"); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.textView); button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run(){ Message message=new Message(); message.what=UPDATE_TEXT; handler.sendMessage(message); } }).start(); } }); } }