1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 @   lizhe313  阅读(128)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示