计数器是指一些常用计时器,例如体育比赛中测试时间的计时器等,但所要介绍的这种计时器一般原理,先让我们看一下图先

 

 让我们看一下代码的实现方法:

 

 
  
  1. package com.smart; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.Context; 
  5. import android.os.Bundle; 
  6. import android.os.Handler; 
  7. import android.view.View; 
  8. import android.view.View.OnClickListener; 
  9. import android.widget.Button; 
  10. import android.widget.TextView; 
  11. import android.widget.Toast; 
  12.  
  13. public class Main extends Activity implements OnClickListener, Runnable{ 
  14.     private Handler handler; 
  15.     private TextView sCount; 
  16.     private int count=0
  17.     @Override//创建方法 
  18.     public void onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState); 
  20.         setContentView(R.layout.main); 
  21.         Button sStart=(Button)findViewById(R.id.sStart); 
  22.         Button sStop=(Button)findViewById(R.id.sStop); 
  23.         Button showToast=(Button)findViewById(R.id.showToast); 
  24.         sCount=(TextView)findViewById(R.id.sCount); 
  25.         sStart.setOnClickListener(this); 
  26.         sStop.setOnClickListener(this); 
  27.         showToast.setOnClickListener(this); 
  28.         handler=new Handler(); 
  29.     } 
  30.  
  31.     //吐丝显示 
  32.     class RunToast implements Runnable{ 
  33.         private Context context; 
  34.         public RunToast(Context context) { 
  35.             this.context = context; 
  36.         } 
  37.         @Override 
  38.         public void run() {//根据时间去显示内容 
  39.             Toast.makeText(context, "15秒后显示信息内容", Toast.LENGTH_LONG).show(); 
  40.              
  41.         } 
  42.          
  43.     } 
  44.      
  45.      
  46.      
  47.     @Override//事件点击 
  48.     public void onClick(View v) { 
  49.         switch (v.getId()) 
  50.         { 
  51.             case R.id.sStart: 
  52.                 handler.postDelayed(this5000); 
  53.                 break
  54.             case R.id.sStop: 
  55.                 handler.removeCallbacks(this); 
  56.                 break
  57.             case R.id.showToast: 
  58.                  
  59.                 handler.postAtTime(new RunToast(this
  60.                 { 
  61.                 }, android.os.SystemClock.uptimeMillis() + 15 * 1000); 
  62.                 break
  63.  
  64.         } 
  65.          
  66.          
  67.     } 
  68.  
  69.  
  70.  
  71.     @Override//线程运行 
  72.     public void run() { 
  73.          
  74.         sCount.setText("Count: "+String.valueOf(++count));//显示计数: 
  75.         handler.postDelayed(this5000); 
  76.      
  77.     } 

main.xml文件写法

 

 
  
  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. <TextView   
  8.     android:id="@+id/sCount" 
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:text="@string/hello" 
  12.     /> 
  13.      
  14.     <Button 
  15.      
  16.     android:id="@+id/sStart" 
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="wrap_content"  
  19.     android:text="开始计数" 
  20.     /> 
  21.     <Button 
  22.      
  23.     android:id="@+id/sStop" 
  24.     android:layout_width="fill_parent"  
  25.     android:layout_height="wrap_content"  
  26.     android:text="停止计数" 
  27.     /> 
  28.      
  29.     <Button 
  30.     android:id="@+id/showToast" 
  31.     android:layout_width="fill_parent"  
  32.     android:layout_height="wrap_content"  
  33.     android:text="15秒后显示Toast信息" 
  34.     /> 
  35. </LinearLayout>