1 package com.qianfeng.gp08_day26_hanlder2;
 2 
 3 import android.os.Bundle;
 4 import android.os.Handler;
 5 import android.app.Activity;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends Activity {
12 
13     private TextView textView;
14     private Button btn1,btn2;
15     private Handler handler = new Handler();
16     private int count;
17     private boolean isStop;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         
23         textView = (TextView) findViewById(R.id.textView);
24         btn1 = (Button) findViewById(R.id.btn1);
25         btn2 = (Button) findViewById(R.id.btn2);
26         
27         btn2.setEnabled(false);
28         
29     }
30 
31     public void startTime(View v)//开始计时
32     {
33         isStop = false;
34         btn1.setEnabled(false);
35         btn2.setEnabled(true);
36         new Thread(new Runnable(){
37            
38             @Override
39             public void run() {
40                 // TODO 向主线程发送计数
41                  count=0;
42                 while(!isStop)
43                 {  
44                     handler.post(new Runnable(){
45                         @Override
46                         public void run() {
47                             textView.setText("计数器:"+count++);
48                         }
49                     });
50                     try {
51                         Thread.sleep(1000);
52                     } catch (InterruptedException e) {
53                         e.printStackTrace();
54                     }
55                 }
56             }
57             
58         }).start();
59     }
60     
61     public void stopTime(View v)
62     {
63         btn1.setEnabled(true);
64         btn2.setEnabled(false);
65         isStop = true;
66     }
67 }
mainActivity

 

posted on 2015-09-19 22:28  毕哥  阅读(193)  评论(0编辑  收藏  举报