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();            }        });    }}
  1. Message是线程之间传递的消息,携带少量信息,int类型的what,arg1,arg2和object类型的obj。
  2. Hander是发送以及处理消息的,发送消息使用sendMessage(),最终处理消息在handleMessage()中。

更多相关文章

  1. Android之handle异步加载图片
  2. 理解Android(安卓)UI线程
  3. android ui 更新 示例
  4. 自定义线程池ThreadPoolExecutor
  5. Android(安卓)StudioHttp协议GET连接
  6. Android线程安全问题分析与解决
  7. java 获取当前所有线程的两种方法 group和map
  8. 融云清空历史消息 Android(安卓)端
  9. Android版的模拟交通灯

随机推荐

  1. 优秀博文
  2. Android解决NDK not configured问题
  3. android 根据SD卡中图片路径读取并显示SD
  4. android studio3.1.3和kotlin1.2.51踩坑(
  5. Android(安卓)自带Apps 学习---AlarmCloc
  6. Android(Java):http options
  7. Android计算器界面布局
  8. Android(安卓)跳转到应用市场,评价App
  9. android 的 setTag
  10. android 通过构造创建进度对话框