package com.hanqi.testapp2;

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;

public class TestActivity5 extends AppCompatActivity {
   TextView tv_5;
    TextView tv_6;
    Button bt_3;
    Button bt_4;
    Button 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 h1=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    bt_3.setEnabled(false);
                    bt_4.setEnabled(true);
                    bt_5.setEnabled(true);
                    if(i==20){
                        return ;
                    }
                    i++;
                    tv_6.setText(i+"");
                    h1.sendEmptyMessageDelayed(1,1000);
                    h1.removeMessages(2);
                    break;
                case 2:
                    bt_3.setEnabled(true);
                    bt_4.setEnabled(false);
                    bt_5.setEnabled(true);
                    if(i==1){
                        return ;
                    }
                    i--;
                    tv_6.setText(i+"");
                    h1.sendEmptyMessageDelayed(2,1000);
                    h1.removeMessages(1);
                    break;
                case 3:
                    bt_3.setEnabled(true);
                    bt_4.setEnabled(true);
                    bt_5.setEnabled(false);
                    h1.removeMessages(1);
                    h1.removeMessages(2);
                    break;


            }
        }
    };



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test5);
        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);
    }



    public void bt6_OnClick(View v){
        switch (v.getId()){
            case R.id.bt_3:
                h1.sendEmptyMessage(1);
                break;
            case R.id.bt_4:
                h1.sendEmptyMessage(2);
                break;
            case R.id.bt_5:
                h1.sendEmptyMessage(3);
                break;
        }
    }
    public void bt2_OnClick(View v){
        new Thread(){
            @Override
            public void run() {
              //发送消息
                //创建消息
                Message m = Message.obtain();
                m.what=1;//id
                m.obj="我传递的信息1";
                //发送即时消息
                h.sendMessage(m);
                m = Message.obtain();
                m.what=1;//id
                m.obj="我传递的信息2";

                //发送延迟消息
                h.sendMessageDelayed(m,2000);
                h.sendEmptyMessage(2);

            }
        }.start();
    }

}

  

posted @ 2016-05-17 14:06  lizhe313  阅读(128)  评论(0编辑  收藏  举报