xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/change" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text"
tools:srcCompat="@tools:sample/avatars[10]" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.threadtest;
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.ImageView;
import android.widget.TextView;
import static java.lang.Thread.sleep;
public class MainActivity extends AppCompatActivity {
public static final int UPDATA = 1;
private TextView textView;
private Button button;
private Handler hander;
private ImageView imageView;
private String[] strings={"1","2","3"};
private int[] imageViews={R.mipmap.a,R.mipmap.b,R.mipmap.c};
int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text);
button=findViewById(R.id.change);
imageView=findViewById(R.id.imageView);
hander=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case UPDATA:
index=index%3;
textView.setText(strings[index]);
imageView.setImageResource(imageViews[index]);
index++;
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Thread thread= new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message message = new Message();
message.what = UPDATA;
hander.sendMessage(message);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
});
}
}
- Message是線程之間傳遞的消息,攜帶少量信息,int類型的what,arg1,arg2和object類型的obj。
- Hander是發送以及處理消息的,發送消息使用sendMessage(),最終處理消息在handleMessage()中。