信息机制

layout代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.my.myapplication.Testduoxianchen">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="城市1"
        android:id="@+id/tv_3"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="城市2"
        android:id="@+id/tv_4"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="bt1_onClick"
        android:text="启动"
        android:id="@+id/bt_1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_5"
        android:text="接收的消息"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发消息"
        android:onClick="bt2_onClick"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:id="@+id/tv_6"
        android:layout_gravity="center"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="增加"
        android:id="@+id/bt_3"
        android:onClick="bt3_onClick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="减少"
        android:id="@+id/bt_4"
        android:onClick="bt3_onClick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="暂停"
        android:id="@+id/bt_5"
        android:onClick="bt3_onClick"
        android:enabled="falsea"/>
</LinearLayout>

Activity代码:

package com.example.my.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class Testduoxianchen extends AppCompatActivity {

    TextView tv_3,tv_4,tv_5,tv_6;
    Button bt_3,bt_4,bt_5;

    //定义Handler

    Handler h=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            // 处理消息
            if(msg.what==1)
            {
                String m=msg.obj.toString();

                tv_5.setText(tv_5.getText()+" "+m);
            }
            else if (msg.what==2)
            {
                tv_5.setText(tv_5.getText()+"空消息");
            }
        }
    };


    int i=10;
    Handler hl=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:
                    if (i==20)
                    {
                        bt_5.setEnabled(false);
                        return;
                    }
                    i++;
                    tv_6.setText(i + "");
                    //发送
                    hl.sendEmptyMessageDelayed(1, 1000);

                    hl.removeMessages(2);
                    bt_3.setEnabled(false);
                    bt_4.setEnabled(true);
                    bt_5.setEnabled(true);
                    break;
                case 2:
                    if (i==1)
                    {
                        bt_5.setEnabled(false);
                        return;
                    }
                    i--;
                    tv_6.setText(i+"");
                    hl.sendEmptyMessageDelayed(2,1000);
                    bt_3.setEnabled(true);
                    bt_4.setEnabled(false);
                    bt_5.setEnabled(true);

                    hl.removeMessages(1);
                    break;
                case 3:
                    hl.removeMessages(1);
                    hl.removeMessages(2);
                    bt_3.setEnabled(true);
                    bt_4.setEnabled(true);
                    bt_5.setEnabled(false);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testduoxianchen);
        tv_3=(TextView)findViewById(R.id.tv_3);
        tv_4=(TextView)findViewById(R.id.tv_4);
        tv_5=(TextView)findViewById(R.id.tv_5);
        tv_6=(TextView)findViewById(R.id.tv_6);
        bt_3=(Button)findViewById(R.id.bt_3);
        bt_4=(Button)findViewById(R.id.bt_4);
        bt_5=(Button)findViewById(R.id.bt_5);
    }
    //3个按钮
    public void bt3_onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.bt_3:
                hl.sendEmptyMessage(1);
                break;
            case R.id.bt_4:
                hl.sendEmptyMessage(2);
                break;
            case R.id.bt_5:
                hl.sendEmptyMessage(3);
                break;
        }
    }


    public void bt2_onClick(View v)
    {
        //启动线程
        new Thread(){
            @Override
            public void run() {
                //发送消息
                //1.创建消息

                Message m=Message.obtain();
                m.what=1;//id
                m.obj="新信息1";

                //2.发送即时消息
                h.sendMessage(m);

                m=Message.obtain();
                m.what=1;//id
                m.obj="新信息2";
                //发送延时消息
                h.sendMessageDelayed(m,2000);

                h.sendEmptyMessage(2);

                h.sendEmptyMessageDelayed(2,3000);
            }
        }.start();

    }


    String c1="北京";
    String c2="青岛";
    public void bt1_onClick(View v)
    {
        new Thread(){
            @Override
            public void run() {

                for(int i=0;i<20;i++)
                {
                    if(i%2==0)
                    {
                        c1="";
                    }
                    else
                    {
                        c1="北京";
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_3.setText(c1);
                        }
                    });
                    try {
                        //暂停 时间随机
                        Thread.sleep(new Random().nextInt(1000));
                    }
                    catch (Exception e)
                    {

                    }

                }
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       Toast.makeText(Testduoxianchen.this, c1 + "循环完成", Toast.LENGTH_SHORT).show();
                   }
               });

            }
        }.start();
        new Thread(){
            @Override
            public void run() {

                for(int i=0;i<20;i++)
                {
                    if(i%2==0)
                    {
                        c2="";
                    }
                    else
                    {
                        c2="青岛";
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_4.setText(c2);
                        }
                    });
                    try {
                        //暂停 时间随机
                        Thread.sleep(new Random().nextInt(1000));
                    }
                    catch (Exception e)
                    {

                    }

                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Testduoxianchen.this, c2 + "循环完成", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        }.start();

    }
}

效果图:

原文地址:https://www.cnblogs.com/beens/p/5501186.html