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

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

package com.smart;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class Main extends Activity implements OnClickListener, Runnable{private Handler handler;private TextView sCount;private int count=0;@Override//创建方法    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button sStart=(Button)findViewById(R.id.sStart);        Button sStop=(Button)findViewById(R.id.sStop);        Button showToast=(Button)findViewById(R.id.showToast);        sCount=(TextView)findViewById(R.id.sCount);        sStart.setOnClickListener(this);        sStop.setOnClickListener(this);        showToast.setOnClickListener(this);        handler=new Handler();}//吐丝显示class RunToast implements Runnable{private Context context;public RunToast(Context context) {this.context = context;}@Overridepublic void run() {//根据时间去显示内容Toast.makeText(context, "15秒后显示信息内容", Toast.LENGTH_LONG).show();}}@Override//事件点击public void onClick(View v) {switch (v.getId()){case R.id.sStart:handler.postDelayed(this, 5000);break;case R.id.sStop:handler.removeCallbacks(this);break;case R.id.showToast:handler.postAtTime(new RunToast(this){}, android.os.SystemClock.uptimeMillis() + 15 * 1000);break;}}@Override//线程运行public void run() {sCount.setText("Count: "+String.valueOf(++count));//显示计数:handler.postDelayed(this, 5000);}}
main.xml文件写法

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView  android:id="@+id/sCount"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />        <Button        android:id="@+id/sStart"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="开始计数"    />    <Button        android:id="@+id/sStop"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="停止计数"    />        <Button    android:id="@+id/showToast"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="15秒后显示Toast信息"    /></LinearLayout>

更多相关文章

  1. Android(安卓)DataPickerDialog组建只显示年月或月日
  2. Android(安卓)启动Activity而不显示界面的问题解决
  3. Android(安卓)PopupWindow显示在控件上方或者下方(demo)
  4. Android(安卓)来电归属地显示功能demo
  5. Android有用的工具类:倒计时器、 ViewHolder简化写法
  6. Android搜索关键字高亮显示
  7. Android麦克风录音带音量大小动态显示的圆形自定义View
  8. Android实现RecyclerView显示json数据并点击跳转页面
  9. Android手动显示和隐藏软键盘方法总结

随机推荐

  1. Android(安卓)处理空列表的方法(必看篇)
  2. Android(安卓)无需申请key直接调用微信/Q
  3. Android Service相关
  4. android 蓝牙打印程序源代码
  5. 下拉列表(Spinner)
  6. android当方法数超过64k时(android studi
  7. android的ListView
  8. android中application标签说明
  9. Android Wi-Fi AutoConnect to Ap take l
  10. Android 复习笔记之图解Actionbar