Android Handler练习

 1 package com.example.myact12;
 2 
 3 
 4 import java.util.Random;
 5 
 6 import android.support.v7.app.ActionBarActivity;
 7 import android.os.Bundle;
 8 import android.os.Handler;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.TextView;
12 /**
13  * Android handler
14  * @Describe: 
15  * @package: com.example.myact12
16  * @author shaobn
17  * @date 2015-9-16 上午10:11:50
18  */
19 public class MainActivity extends ActionBarActivity {
20     private Button start;
21     private Button end;
22     private TextView textView;
23     private Handler handler = new Handler();
24         
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29         start = (Button) this.findViewById(R.id.button1);
30         end = (Button) this.findViewById(R.id.button2);
31         textView = (TextView) this.findViewById(R.id.textView1);
32         start.setOnClickListener(new ButtonClick());
33         end.setOnClickListener(new ButtonClick());
34     }
35     class ButtonClick implements View.OnClickListener{
36         @Override
37         public void onClick(View arg0) {
38             // TODO Auto-generated method stub
39                 switch (arg0.getId()) {
40                 case R.id.button1:
41                     Runnable runnable = new Runnable() {
42                         
43                         @Override
44                         public void run() {
45                             // TODO Auto-generated method stub
46                             Random random = new Random();
47                             final int num = random.nextInt(100);
48                             Runnable runnable2 = new Runnable() {
49                                 
50                                 @Override
51                                 public void run() {
52                                     // TODO Auto-generated method stub
53                                     try {
54                                         Thread.sleep(1000);
55                                     } catch (Exception e) {
56                                         // TODO: handle exception
57                                         e.printStackTrace();
58                                     }
59                                     
60                                     MainActivity.this.textView.setText(String.valueOf(num));
61                                 }
62                             };
63                             
64                             MainActivity.this.handler.post(runnable2);
65                         }
66                     };
67                     new Thread(runnable).start();
68                     break;
69 
70                 case R.id.button2:
71                     Runnable runnable2 = new Runnable() {
72                         
73                         @Override
74                         public void run() {
75                             // TODO Auto-generated method stub
76                             
77                         }
78                     };
79                     break;
80                 }
81                 
82             
83         }
84         
85         
86     }
87 }

 

posted @ 2015-09-16 10:13  邻家小书童  阅读(175)  评论(0编辑  收藏  举报