handleMessage的使用
xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="36dp"android:text="hello_world,好久不见" /><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/tv"android:layout_centerHorizontal="true"android:layout_marginTop="19dp"android:text="下一条" /></RelativeLayout>
java代码:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final TextView tv=(TextView) findViewById(R.id.tv);Button btn=(Button)findViewById(R.id.btn);final Handler handler=new Handler(){public void handleMessage(android.os.Message msg) {super.handleMessage(msg);if(msg.what==0x123){tv.setText("下一次");}};};btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {Thread thread=new Thread(new Runnable() {@Overridepublic void run() {handler.sendEmptyMessage(0x123);}});thread.start();}});}
}