安卓学习之多线程

看视频后自己编的,记录下来,以后忘了直接代码就行了。

java类:

View Code
 1 package tk.handleractivity;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.os.Handler;

 6 import android.view.Menu;

 7 import android.view.View;

 8 import android.view.View.OnClickListener;

 9 import android.widget.Button;

10 

11 

12 

13 public class MyHangler extends Activity {

14 

15     private Button startButton=null;

16     private Button endButton=null;

17     

18     @Override

19     public void onCreate(Bundle savedInstanceState) {

20         super.onCreate(savedInstanceState);

21         setContentView(R.layout.myhangler);

22         startButton=(Button)findViewById(R.id.startButton);

23         startButton.setOnClickListener(new StartbuttonListener());

24         endButton=(Button)findViewById(R.id.endButton);

25         endButton.setOnClickListener(new EndbuttonListener());

26         

27         

28     }

29 

30     class StartbuttonListener implements OnClickListener{

31         @Override

32         public void onClick(View v) {

33             handler.post(updateThead);

34         }        

35     }

36     class EndbuttonListener implements OnClickListener{

37         @Override

38         public void onClick(View v) {

39             handler.removeCallbacks(updateThead);

40         }        

41     }

42     

43     @Override

44     public boolean onCreateOptionsMenu(Menu menu) {

45         getMenuInflater().inflate(R.menu.myhangler, menu);

46         return true;

47     }

48     Handler handler=new Handler();

49     Runnable updateThead=new Runnable() {

50         

51         @Override

52         public void run() {

53             System.out.println("UpdateThead");

54             handler.postDelayed(updateThead, 3000);

55         }

56     };

57 }

 

XML布局文件:

View Code
 1 <?xml version= "1.0" encoding ="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:orientation="vertical"

 4     android:layout_width= "fill_parent"

 5     android:layout_height= "fill_parent"

 6     >

 7 

 8     <Button

 9         android:id="@+id/startButton"

10         android:layout_width= "fill_parent"

11         android:layout_height= "wrap_content"

12         android:text="start"

13         />

14      <Button

15         android:id="@+id/endButton"

16         android:layout_width= "fill_parent"

17         android:layout_height= "wrap_content"

18         android:text="end"

19         />

20     

21     

22     

23 </LinearLayout>

 

你可能感兴趣的:(多线程)