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

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

        
  1. packagecom.smart;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.os.Bundle;
  5. importandroid.os.Handler;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.widget.Button;
  9. importandroid.widget.TextView;
  10. importandroid.widget.Toast;
  11. publicclassMainextendsActivityimplementsOnClickListener,Runnable{
  12. privateHandlerhandler;
  13. privateTextViewsCount;
  14. privateintcount=0;
  15. @Override//创建方法
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. ButtonsStart=(Button)findViewById(R.id.sStart);
  20. ButtonsStop=(Button)findViewById(R.id.sStop);
  21. ButtonshowToast=(Button)findViewById(R.id.showToast);
  22. sCount=(TextView)findViewById(R.id.sCount);
  23. sStart.setOnClickListener(this);
  24. sStop.setOnClickListener(this);
  25. showToast.setOnClickListener(this);
  26. handler=newHandler();
  27. }
  28. //吐丝显示
  29. classRunToastimplementsRunnable{
  30. privateContextcontext;
  31. publicRunToast(Contextcontext){
  32. this.context=context;
  33. }
  34. @Override
  35. publicvoidrun(){//根据时间去显示内容
  36. Toast.makeText(context,"15秒后显示信息内容",Toast.LENGTH_LONG).show();
  37. }
  38. }
  39. @Override//事件点击
  40. publicvoidonClick(Viewv){
  41. switch(v.getId())
  42. {
  43. caseR.id.sStart:
  44. handler.postDelayed(this,5000);
  45. break;
  46. caseR.id.sStop:
  47. handler.removeCallbacks(this);
  48. break;
  49. caseR.id.showToast:
  50. handler.postAtTime(newRunToast(this)
  51. {
  52. },android.os.SystemClock.uptimeMillis()+15*1000);
  53. break;
  54. }
  55. }
  56. @Override//线程运行
  57. publicvoidrun(){
  58. sCount.setText("Count:"+String.valueOf(++count));//显示计数:
  59. handler.postDelayed(this,5000);
  60. }
  61. }

main.xml文件写法

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <Button
  14. android:id="@+id/sStart"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="开始计数"
  18. />
  19. <Button
  20. android:id="@+id/sStop"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="停止计数"
  24. />
  25. <Button
  26. android:id="@+id/showToast"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="15秒后显示Toast信息"
  30. />
  31. </LinearLayout>

更多相关文章

  1. Android系列教程之六:TextView小组件的使用--附带超链接和跑马灯
  2. Android的onCreateOptionsMenu()创建菜单Menu详解
  3. android 界面布局 很好的一篇总结
  4. android应用程序跳转到系统的各个设置页面
  5. Android控件Editext、TextView属性详解
  6. Android开发:还是Menu
  7. android强制横屏息屏后重新打开时会先显示竖屏
  8. Android下如何计算要显示的字符串所占的宽度和高度
  9. Android(安卓)adb devices显示no permission

随机推荐

  1. android添加以太网ethernet方法 android
  2. Android中的GPS应用开发入门教程
  3. Android应用程序框架层和系统运行库层日
  4. Eclipse开发Android常用快捷键
  5. android studio将一个项目作为module(libr
  6. Android Studio--Android中的消息机制
  7. Understanding Android Security(安卓安全
  8. 正确的Flutter和Android交互代码
  9. android进程间服务通信
  10. 关于android的9path图片处理