成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android開發:Chronometer控件實現計時器

移動開發 Android
使用Chronometer控件實現計器的操作。通過設置setBase(long base)來設置初始時間,然后為其添加一 個 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件來判斷時間是否到了,然后再調用其stop()方法實現停止計時。

本文為大家演示了如何使用Chronometer控件實現Android計時器的實例。

先貼上最終的實現效果圖:

[[73425]]

[[73426]]

Android計時器實現思路

使用Chronometer控件實現計器的操作。通過設置setBase(long base)來設置初始時間,然后為其添加一 個 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件來判斷時間是否到了,然后再調用其stop()方法實現停止計時。

Android計時器實現代碼

main.xml:

XML/HTML代碼

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent" 
  5. android:background="@drawable/back" 
  6. android:gravity="center" 
  7. android:orientation="vertical"> 
  8. <LinearLayout 
  9. android:layout_width="fill_parent" 
  10. android:layout_height="wrap_content" 
  11. android:layout_margin="10dip" 
  12. android:orientation="horizontal"> 
  13. <TextView 
  14. android:layout_width="fill_parent" 
  15. android:layout_height="wrap_content" 
  16. android:layout_weight="4" 
  17. android:gravity="center" 
  18. android:text="設置時間:"/> 
  19. <EditText 
  20. android:id="@+id/edt_settime" 
  21. android:layout_width="fill_parent" 
  22. android:layout_height="wrap_content" 
  23. android:layout_weight="1" 
  24. android:inputType="number"/> 
  25. </LinearLayout> 
  26. <Chronometer 
  27. android:id="@+id/chronometer" 
  28. android:layout_width="fill_parent" 
  29. android:layout_height="wrap_content" 
  30. android:gravity="center" 
  31. android:textColor="#ff0000" 
  32. android:textSize="60dip"/> 
  33. <LinearLayout 
  34. android:layout_width="fill_parent" 
  35. android:layout_height="wrap_content" 
  36. android:layout_margin="10dip" 
  37. android:orientation="horizontal"> 
  38. <Button 
  39. android:id="@+id/btnStart" 
  40. android:layout_width="fill_parent" 
  41. android:layout_height="wrap_content" 
  42. android:layout_weight="1" 
  43. android:text="開始記時"/> 
  44. <Button 
  45. android:id="@+id/btnStop" 
  46. android:layout_width="fill_parent" 
  47. android:layout_height="wrap_content" 
  48. android:layout_weight="1" 
  49. android:text="停止記時"/> 
  50. <Button 
  51. android:id="@+id/btnReset" 
  52. android:layout_width="fill_parent" 
  53. android:layout_height="wrap_content" 
  54. android:layout_weight="1" 
  55. android:text="重置"/> 
  56. </LinearLayout> 
  57. </LinearLayout> 

Activity代碼:

Java代碼

  1.     package com.jiahui.chronometer;    
  2.     import android.app.Activity;    
  3.     import android.app.AlertDialog;    
  4.     import android.app.Dialog;    
  5.     import android.content.DialogInterface;    
  6.     import android.os.Bundle;    
  7.     import android.os.SystemClock;    
  8.     import android.text.format.Time;    
  9.     import android.view.View;    
  10.     import android.widget.Button;    
  11.     import android.widget.Chronometer;    
  12.     import android.widget.EditText;    
  13.     publicclass ChronometerDemoActivity extends Activity {    
  14.     privateint startTime = 0;    
  15.     publicvoid onCreate(Bundle savedInstanceState) {    
  16.     super.onCreate(savedInstanceState);    
  17.             setContentView(R.layout.main);    
  18.     final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);    
  19.             Button btnStart = (Button) findViewById(R.id.btnStart);    
  20.             Button btnStop = (Button) findViewById(R.id.btnStop);    
  21.             Button btnRest = (Button) findViewById(R.id.btnReset);    
  22.     final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);    
  23.             btnStart.setOnClickListener(new View.OnClickListener() {    
  24.     @Override 
  25.     publicvoid onClick(View v) {    
  26.                     System.out.println("--開始記時---");    
  27.                     String ss = edtSetTime.getText().toString();    
  28.     if (!(ss.equals("") && ss != null)) {    
  29.                         startTime = Integer.parseInt(edtSetTime.getText()    
  30.                                 .toString());    
  31.                     }    
  32.     // 設置開始講時時間  
  33. chronometer.setBase(SystemClock.elapsedRealtime());    
  34.     // 開始記時  
  35.                     chronometer.start();    
  36.                 }    
  37.             });    
  38.             btnStop.setOnClickListener(new View.OnClickListener() {    
  39.     @Override 
  40.     publicvoid onClick(View v) {    
  41.     // 停止  
  42.                     chronometer.stop();    
  43.                 }    
  44.             });    
  45.     // 重置  
  46.             btnRest.setOnClickListener(new View.OnClickListener() {    
  47.     @Override 
  48.     publicvoid onClick(View v) {    
  49. chronometer.setBase(SystemClock.elapsedRealtime());    
  50.                 }    
  51.             });    
  52.             chronometer    
  53.                     .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {    
  54.     @Override 
  55.     publicvoid onChronometerTick(Chronometer chronometer) {    
  56.     // 如果開始計時到現在超過了startime秒  
  57.     if (SystemClock.elapsedRealtime()    
  58.                                     - chronometer.getBase() > startTime * 1000) {    
  59.                                 chronometer.stop();    
  60.     // 給用戶提示  
  61.                                 showDialog();    
  62.                             }    
  63.                         }    
  64.                     });    
  65.         }    
  66.     protectedvoid showDialog() {    
  67.             AlertDialog.Builder builder = new AlertDialog.Builder(this);    
  68.              builder.setIcon(R.drawable.eb28d25);    
  69.             builder.setTitle("警告").setMessage("時間到")    
  70.                     .setPositiveButton("確定"new DialogInterface.OnClickListener() {    
  71.     @Override 
  72.     publicvoid onClick(DialogInterface dialog, int which) {    
  73.                         }    
  74.                     });    
  75.             AlertDialog dialog = builder.create();    
  76.             dialog.show();    
  77.         }    
  78.     }  
責任編輯:閆佳明 來源: jizhuomi
相關推薦

2024-01-25 08:24:23

場景編程方式控制計時

2011-05-31 16:50:35

Android 線程

2011-09-08 14:01:01

Android Wid實例

2023-04-17 09:08:27

CSS計時器

2012-05-08 13:58:37

SharePoint

2010-01-25 11:29:33

Android計時器

2023-01-11 09:02:50

2021-11-26 00:04:20

Go計時器重構

2013-03-25 10:03:35

網絡優化網絡抑制快速認知網絡

2020-06-11 08:48:49

JavaScript開發技術

2022-06-28 15:29:56

Python編程語言計時器

2023-12-11 09:50:35

Linux定時器

2010-01-05 15:00:30

.NET Framew

2022-06-23 07:23:34

自定義組件計時器

2020-03-10 09:42:04

JavaScript前端線程

2022-06-30 16:10:26

Python計時器裝飾器

2021-12-07 11:30:32

Go煮蛋計時器

2019-12-24 16:52:22

Go語言騰訊TM函數

2024-07-18 08:46:58

.NET輕量級計時器測量代碼塊

2022-06-29 14:15:01

Python計時器上下文管理器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人在线免费看 | 欧美一二三四成人免费视频 | 国产成人在线一区二区 | 91高清在线观看 | 精品久久久久久亚洲精品 | 天堂综合| 精品国产乱码久久久久久闺蜜 | 日韩在线免费视频 | www操操 | 人人鲁人人莫人人爱精品 | 国产四区 | xx性欧美肥妇精品久久久久久 | 亚洲精品电影 | 中文字幕精品一区 | 亚洲一区二区三区免费在线观看 | 999久久久| 国产精品1区2区 | 国产精品高清一区二区三区 | 一区二区av | 美女爽到呻吟久久久久 | 高清国产午夜精品久久久久久 | 国产福利视频 | 国产精品v | 成人小视频在线 | 日韩av福利在线观看 | 2019天天干夜夜操 | 精品久久久久久久久久 | 欧美精品一区二区三区在线 | 国产亚洲成av人片在线观看桃 | 日韩综合网 | 在线观看黄色电影 | 欧美激情综合色综合啪啪五月 | 欧美日韩国产中文 | 在线国产小视频 | 国产精品久久久久久久久大全 | 国产精品久久久久久av公交车 | 久久精品国产亚洲夜色av网站 | 黄色在线观看网址 | 欧美自拍一区 | 91免费入口| 国产乱码精品一区二区三区中文 |