自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android開發(fā):Chronometer控件實現(xiàn)計時器

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

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

先貼上最終的實現(xiàn)效果圖:

[[73425]]

[[73426]]

Android計時器實現(xiàn)思路

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

Android計時器實現(xiàn)代碼

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="設(shè)置時間:"/> 
  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.     // 設(shè)置開始講時時間  
  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.     // 如果開始計時到現(xiàn)在超過了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.     }  
責(zé)任編輯:閆佳明 來源: jizhuomi
相關(guān)推薦

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計時器

2010-01-25 11:29:33

Android計時器

2012-05-08 13:58:37

SharePoint

2021-11-26 00:04:20

Go計時器重構(gòu)

2023-01-11 09:02:50

2013-03-25 10:03:35

網(wǎng)絡(luò)優(yōu)化網(wǎng)絡(luò)抑制快速認(rèn)知網(wǎng)絡(luò)

2020-06-11 08:48:49

JavaScript開發(fā)技術(shù)

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前端線程

2021-12-07 11:30:32

Go煮蛋計時器

2019-12-24 16:52:22

Go語言騰訊TM函數(shù)

2022-06-30 16:10:26

Python計時器裝飾器

2024-07-18 08:46:58

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

2022-06-19 20:48:06

樹莓派Linux
點贊
收藏

51CTO技術(shù)棧公眾號