Android&Handler2

ActivityMain.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" 
10     android:orientation="vertical">
11 
12     <TextView
13         android:id="@+id/myTextView"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="@string/hello_world" />
17     <ProgressBar
18         android:id="@+id/bar"
19         android:layout_width="200dp"
20         android:layout_height="40dp"
21         style="?android:attr/progressBarStyleHorizontal"
22         android:visibility="gone"
23         android:max="100"
24         android:layout_gravity="center_vertical"
25         />
26     <Button
27         android:id="@+id/startButton"
28         android:layout_width="fill_parent"
29         android:layout_height="wrap_content"
30         android:text="Start"
31         />
32     <Button
33         android:id="@+id/endButton"
34         android:layout_width="fill_parent"
35         android:layout_height="wrap_content"
36         android:text="End"
37         />"
38 </LinearLayout>
MainActivity.java
 1 package com.example.handleactivity;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.os.Message;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.ProgressBar;
12 
13 public class MainActivity extends Activity {
14 
15     private Button startButton = null;
16     private Button endButton = null;
17     private ProgressBar bar = null;
18     private MyHandler myHandler = new MyHandler();
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         startButton = (Button)findViewById(R.id.startButton);
24         endButton = (Button)findViewById(R.id.endButton);
25         bar = (ProgressBar)findViewById(R.id.bar);
26         
27         startButton.setOnClickListener(new OnClickListener(){
28 
29             @Override
30             public void onClick(View arg0) {
31                 // TODO Auto-generated method stub
32                 bar.setVisibility(View.VISIBLE);
33                 myHandler.post(myThread);
34             }
35             
36         });
37         
38         endButton.setOnClickListener(new OnClickListener(){
39 
40             @Override
41             public void onClick(View v) {
42                 // TODO Auto-generated method stub
43                 myHandler.removeCallbacks(myThread);
44             }
45             
46         });
47     }
48 
49     @Override
50     public boolean onCreateOptionsMenu(Menu menu) {
51         // Inflate the menu; this adds items to the action bar if it is present.
52         getMenuInflater().inflate(R.menu.main, menu);
53         return true;
54     }
55     
56     class MyHandler extends Handler{
57 
58         public void handleMessage(Message msg) {
59             // TODO Auto-generated method stub
60             super.handleMessage(msg);
61             bar.setProgress(msg.arg1);
62             this.post(myThread);
63         }
64         
65     }
66     Runnable myThread = new Runnable(){
67         private int count = 0;
68         @Override
69         public void run() {
70             // TODO Auto-generated method stub
71             if(count<100){
72                 count += 10;
73             Message msg = myHandler.obtainMessage();
74             msg.arg1 = count;
75             try {
76                 Thread.sleep(1000);
77             } catch (InterruptedException e) {
78                 // TODO Auto-generated catch block
79                 e.printStackTrace();
80             }
81             myHandler.sendMessage(msg);
82             }
83             else{
84                 myHandler.removeCallbacks(this);
85             }
86         }
87         
88         
89     };
90     
91 }

 

posted on 2013-03-25 20:51  Zachary_wiz  阅读(142)  评论(0编辑  收藏  举报

导航