ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一、运行结果

一、代码
1.xml
(1)activity_main.xml

 1 <RelativeLayout 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="com.example.s01_original_e14_simplehandler.MainActivity" >
10     
11     <Button 
12         android:id="@+id/startThread"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/startThread"/>
16     
17      <Button 
18      android:id="@+id/stopThread"
19      android:layout_width="wrap_content"
20      android:layout_height="wrap_content"
21      android:text="@string/stopThread"
22      android:layout_below="@id/startThread"/>
23      
24      <TextView 
25          android:id="@+id/myTextView"
26          android:layout_width="wrap_content"
27          android:layout_height="wrap_content"
28          android:layout_below="@id/stopThread"/>
29 
30 </RelativeLayout>

 

2.java
(1)MainActivity.java

 1 package com.example.s01_original_e14_simplehandler;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends Activity {
12 
13     private Button startThread = null;
14     private Button stopThread = null;
15     private TextView myTextView = null;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         myTextView = (TextView) findViewById(R.id.myTextView);
21         startThread = (Button) findViewById(R.id.startThread);
22         stopThread = (Button) findViewById(R.id.stopThread);
23         
24         startThread.setOnClickListener(new OnClickListener() {
25             @Override
26             public void onClick(View v) {
27                 handler.post(updateThread);
28             }
29         });
30         
31         stopThread.setOnClickListener(new OnClickListener() {
32             @Override
33             public void onClick(View v) {
34                 handler.removeCallbacks(updateThread);
35             }
36         });
37     }
38     
39     Handler handler = new Handler();
40     
41     Runnable updateThread = new Runnable() {
42         @Override
43         public void run() {
44             System.out.println("---updateThread");
45             myTextView.setText(System.currentTimeMillis()+"");
46             handler.postDelayed(updateThread, 2000);
47         }
48     };
49     
50 }

 

 

posted @ 2016-02-13 18:10  shamgod  阅读(252)  评论(0编辑  收藏  举报
haha