码迷,mamicode.com
首页 > 移动开发 > 详细

Android ——Handler相关

时间:2016-05-17 00:37:13      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.PractiseActivity4"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:id="@+id/tvw1"/>
17     <TextView
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:id="@+id/tvw2"/>
21 
22     <Button
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:id="@+id/btt1"
26         android:text="随机选择"
27         android:onClick="btn_onClick"/>
28     <TextView
29         android:layout_width="wrap_content"
30         android:layout_height="wrap_content"
31         android:text="接受的消息"
32         android:id="@+id/tv_5"/>
33     <Button
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content"
36         android:text="发消息"
37         android:onClick="btn2_onClick"/>
38     <TextView
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:text="10"
42         android:id="@+id/tv_6"
43         android:layout_gravity="center"/>
44     <Button
45         android:layout_width="match_parent"
46         android:layout_height="wrap_content"
47         android:text="增加"
48         android:onClick="btn3_onClick"
49         android:id="@+id/bt_3"/>
50     <Button
51         android:layout_width="match_parent"
52         android:layout_height="wrap_content"
53         android:text="减少"
54         android:onClick="btn3_onClick"
55         android:id="@+id/bt_4"/>
56     <Button
57         android:layout_width="match_parent"
58         android:layout_height="wrap_content"
59         android:text="暂停"
60         android:onClick="btn3_onClick"
61         android:id="@+id/bt_5"/>
62 </LinearLayout>

java类:

  1 package com.hanqi.testapp2;
  2 
  3 import android.os.Bundle;
  4 import android.os.Handler;
  5 import android.os.Message;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10 
 11 public class PractiseActivity4 extends AppCompatActivity {
 12 
 13     TextView tvw1;
 14     Button btt1;
 15     TextView tvw2;
 16     TextView tv_5;
 17     TextView tv_6;
 18     Button bt_3;
 19     Button bt_4;
 20     Button bt_5;
 21     //定义Handler
 22     Handler h = new Handler(){
 23         @Override
 24         public void handleMessage(Message msg) {
 25             super.handleMessage(msg);
 26             //处理消息
 27             if (msg.what ==1)
 28             {
 29                 String m = msg.obj.toString();
 30                 tv_5.setText(tv_5.getText()+" "+m);
 31             }
 32             else if (msg.what ==2)
 33             {
 34                 tv_5.setText(tv_5.getText()+"空消息");
 35             }
 36         }
 37     };
 38 
 39     int i = 10;
 40     Handler hl = new Handler()
 41     {
 42         @Override
 43         public void handleMessage(Message msg) {
 44             super.handleMessage(msg);
 45             switch (msg.what)
 46             {
 47                 case 1:
 48                     if (i ==20)
 49                     {
 50                         return;
 51                     }
 52                     i++;
 53                     tv_6.setText(i + "");
 54                     //发送
 55                     hl.sendEmptyMessageDelayed(1, 1000);
 56                     hl.removeMessages(2);
 57                     bt_3.setEnabled(false);
 58                     bt_4.setEnabled(true);
 59                     bt_5.setEnabled(true);
 60                     break;
 61                 case 2:
 62                     if (i ==0)
 63                     {
 64                         return;
 65                     }
 66                     i--;
 67                     tv_6.setText(i+"");
 68                     hl.sendEmptyMessageDelayed(2, 1000);
 69                     hl.removeMessages(1);
 70                     bt_3.setEnabled(true);
 71                     bt_4.setEnabled(false);
 72                     bt_5.setEnabled(true);
 73                     break;
 74                 case 3:
 75                     hl.removeMessages(1);
 76                     hl.removeMessages(2);
 77                     bt_3.setEnabled(true);
 78                     bt_4.setEnabled(true);
 79                     bt_5.setEnabled(false);
 80                     break;
 81             }
 82         }
 83     };
 84 
 85     @Override
 86     protected void onCreate(Bundle savedInstanceState) {
 87         super.onCreate(savedInstanceState);
 88         setContentView(R.layout.activity_practise4);
 89         tvw1 = (TextView)findViewById(R.id.tvw1);
 90         btt1 = (Button)findViewById(R.id.btt1);
 91         tvw2 = (TextView)findViewById(R.id.tvw2);
 92         tv_5 = (TextView)findViewById(R.id.tv_5);
 93         tv_6 = (TextView)findViewById(R.id.tv_6);
 94         bt_3 = (Button)findViewById(R.id.bt_3);
 95         bt_4 = (Button)findViewById(R.id.bt_4);
 96         bt_5 = (Button)findViewById(R.id.bt_5);
 97     }
 98     //三个按钮
 99     public void btn3_onClick(View v)
100     {
101        switch (v.getId())
102        {
103            case R.id.bt_3:
104                //发送增加消息
105                hl.sendEmptyMessage(1);
106                break;
107            case R.id.bt_4:
108                hl.sendEmptyMessage(2);
109                break;
110            case R.id.bt_5:
111                hl.sendEmptyMessage(3);
112                break;
113        }
114     }
115     //发消息
116     public void btn2_onClick(View v)
117     {
118         //启动线程
119         new Thread(){
120             @Override
121             public void run() {
122                 //发送消息
123                 //1.创建Message
124                 Message m = Message.obtain();
125                 m.what = 1;//id
126                 m.obj = "新信息";
127                 //2.发送即时消息
128                 h.sendMessage(m);
129                 m = Message.obtain();
130                 m.what = 1;//id
131                 m.obj = "新信息";
132                 //发送延时消息
133                 h.sendMessageDelayed(m,2000);
134                 h.sendEmptyMessage(2);
135                 h.sendEmptyMessageDelayed(2,1000);
136             }
137         }.start();
138     }
139 
140 //    String c1 = "北京";
141 //    String c2 = "上海";
142 //    public void btn_onClick(View v)
143 //    {
144 //        //创建子线程1
145 //       new Thread(){
146 //           @Override
147 //           public void run() {
148 //               for (int i=0;i<20;i++)
149 //               {
150 //                   if(i%2==0)
151 //                   {
152 //                     c1="";
153 //                   }
154 //                   else
155 //                   {
156 //                     c1="北京";
157 //                   }
158 //                   runOnUiThread(new Runnable() {
159 //                       @Override
160 //                       public void run() {
161 //                           tvw1.setText(c1);
162 //                       }
163 //                   });
164 //                   //暂停
165 //                   try {
166 //                       Thread.sleep((int) (Math.random()*1000));
167 //                   }
168 //                  catch (Exception e)
169 //                  {
170 //
171 //                  }
172 //               }
173 //               runOnUiThread(new Runnable() {
174 //                   @Override
175 //                   public void run() {
176 //                       Toast.makeText(PractiseActivity4.this, c1 + "循环完成", Toast.LENGTH_SHORT).show();
177 //                   }
178 //               });
179 //           }
180 //       }.start();
181 //        //创建子线程2
182 //        new Thread(){
183 //            @Override
184 //            public void run() {
185 //                for (int i=0;i<20;i++)
186 //                {
187 //                    if(i%2==0)
188 //                    {
189 //                        c2="";
190 //                    }
191 //                    else
192 //                    {
193 //                        c2="上海";
194 //                    }
195 //                    runOnUiThread(new Runnable() {
196 //                        @Override
197 //                        public void run() {
198 //                            tvw2.setText(c2);
199 //                        }
200 //                    });
201 //                    //暂停
202 //                    try {
203 //                        Thread.sleep((int) (Math.random()*1000));
204 //                    }
205 //                    catch (Exception e)
206 //                    {
207 //
208 //                    }
209 //                }
210 //                runOnUiThread(new Runnable() {
211 //                    @Override
212 //                    public void run() {
213 //                        Toast.makeText(PractiseActivity4.this, c2 + "循环完成", Toast.LENGTH_SHORT).show();
214 //                    }
215 //                });
216 //            }
217 //        }.start();
218 //    }
219 }

 

效果为:

技术分享技术分享技术分享技术分享

Android ——Handler相关

标签:

原文地址:http://www.cnblogs.com/hanazawalove/p/5500069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!