日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android開發(fā):Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器

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

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、君山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

Android計(jì)時(shí)器實(shí)現(xiàn)思路

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

Android計(jì)時(shí)器實(shí)現(xiàn)代碼

main.xml:

XML/HTML代碼

 
 
  1.  
  2. android:layout_width="fill_parent" 
  3. android:layout_height="fill_parent" 
  4. android:background="@drawable/back" 
  5. android:gravity="center" 
  6. android:orientation="vertical"> 
  7. android:layout_width="fill_parent" 
  8. android:layout_height="wrap_content" 
  9. android:layout_margin="10dip" 
  10. android:orientation="horizontal"> 
  11. android:layout_width="fill_parent" 
  12. android:layout_height="wrap_content" 
  13. android:layout_weight="4" 
  14. android:gravity="center" 
  15. android:text="設(shè)置時(shí)間:"/> 
  16. android:id="@+id/edt_settime" 
  17. android:layout_width="fill_parent" 
  18. android:layout_height="wrap_content" 
  19. android:layout_weight="1" 
  20. android:inputType="number"/> 
  21.  
  22. android:id="@+id/chronometer" 
  23. android:layout_width="fill_parent" 
  24. android:layout_height="wrap_content" 
  25. android:gravity="center" 
  26. android:textColor="#ff0000" 
  27. android:textSize="60dip"/> 
  28. android:layout_width="fill_parent" 
  29. android:layout_height="wrap_content" 
  30. android:layout_margin="10dip" 
  31. android:orientation="horizontal"> 
  32. android:id="@+id/btnStart" 
  33. android:layout_width="fill_parent" 
  34. android:layout_height="wrap_content" 
  35. android:layout_weight="1" 
  36. android:text="開始記時(shí)"/> 
  37. android:id="@+id/btnStop" 
  38. android:layout_width="fill_parent" 
  39. android:layout_height="wrap_content" 
  40. android:layout_weight="1" 
  41. android:text="停止記時(shí)"/> 
  42. android:id="@+id/btnReset" 
  43. android:layout_width="fill_parent" 
  44. android:layout_height="wrap_content" 
  45. android:layout_weight="1" 
  46. android:text="重置"/> 
  47.  
  48.  

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("--開始記時(shí)---");    
  27.                     String ss = edtSetTime.getText().toString();    
  28.     if (!(ss.equals("") && ss != null)) {    
  29.                         startTime = Integer.parseInt(edtSetTime.getText()    
  30.                                 .toString());    
  31.                     }    
  32.     // 設(shè)置開始講時(shí)時(shí)間  
  33. chronometer.setBase(SystemClock.elapsedRealtime());    
  34.     // 開始記時(shí)  
  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.     // 如果開始計(jì)時(shí)到現(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("時(shí)間到")    
  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.     }  

新聞標(biāo)題:Android開發(fā):Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器
鏈接分享:http://www.dlmjj.cn/article/dpgijpc.html