zoukankan      html  css  js  c++  java
  • Chronometer控件实现的Android计时器

        本文为大家演示了如何使用Chronometer控件实现Android计时器的实例。

           先贴上最终的实现效果图:

    Android炫酷计时器

    Android炫酷计时器-时间到的提示

           Android计时器实现思路

           使用Chronometer控件实现计器的操作。通过设置setBase(long base)来设置初始时间,然后为其添加一个 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件来判断时间是否到了,然后再调用其stop()方法实现停止计时。

           Android计时器实现代码

           main.xml:

    XML/HTML代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns: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.   
    9.     <LinearLayout  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:layout_margin="10dip"  
    13.         android:orientation="horizontal" >  
    14.   
    15.         <TextView  
    16.             android:layout_width="fill_parent"  
    17.             android:layout_height="wrap_content"  
    18.             android:layout_weight="4"  
    19.             android:gravity="center"  
    20.             android:text="设置时间:" />  
    21.   
    22.         <EditText  
    23.             android:id="@+id/edt_settime"  
    24.             android:layout_width="fill_parent"  
    25.             android:layout_height="wrap_content"  
    26.             android:layout_weight="1"  
    27.             android:inputType="number" />  
    28.     </LinearLayout>  
    29.   
    30.     <Chronometer  
    31.         android:id="@+id/chronometer"  
    32.         android:layout_width="fill_parent"  
    33.         android:layout_height="wrap_content"  
    34.         android:gravity="center"  
    35.         android:textColor="#ff0000"  
    36.         android:textSize="60dip" />  
    37.   
    38.     <LinearLayout  
    39.         android:layout_width="fill_parent"  
    40.         android:layout_height="wrap_content"  
    41.         android:layout_margin="10dip"  
    42.         android:orientation="horizontal" >  
    43.   
    44.         <Button  
    45.             android:id="@+id/btnStart"  
    46.             android:layout_width="fill_parent"  
    47.             android:layout_height="wrap_content"  
    48.             android:layout_weight="1"  
    49.             android:text="开始记时" />  
    50.   
    51.         <Button  
    52.             android:id="@+id/btnStop"  
    53.             android:layout_width="fill_parent"  
    54.             android:layout_height="wrap_content"  
    55.             android:layout_weight="1"  
    56.             android:text="停止记时" />  
    57.   
    58.         <Button  
    59.             android:id="@+id/btnReset"  
    60.             android:layout_width="fill_parent"  
    61.             android:layout_height="wrap_content"  
    62.             android:layout_weight="1"  
    63.             android:text="重置" />  
    64.     </LinearLayout>  
    65.   
    66. </LinearLayout>  

           Activity代码:

    Java代码
    1. package com.jiahui.chronometer;   
    2.   
    3. import android.app.Activity;   
    4. import android.app.AlertDialog;   
    5. import android.app.Dialog;   
    6. import android.content.DialogInterface;   
    7. import android.os.Bundle;   
    8. import android.os.SystemClock;   
    9. import android.text.format.Time;   
    10. import android.view.View;   
    11. import android.widget.Button;   
    12. import android.widget.Chronometer;   
    13. import android.widget.EditText;   
    14.   
    15. public class ChronometerDemoActivity extends Activity {   
    16.   
    17.     private int startTime = 0;   
    18.   
    19.     public void onCreate(Bundle savedInstanceState) {   
    20.         super.onCreate(savedInstanceState);   
    21.         setContentView(R.layout.main);   
    22.   
    23.         final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);   
    24.   
    25.         Button btnStart = (Button) findViewById(R.id.btnStart);   
    26.   
    27.         Button btnStop = (Button) findViewById(R.id.btnStop);   
    28.   
    29.         Button btnRest = (Button) findViewById(R.id.btnReset);   
    30.   
    31.         final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);   
    32.   
    33.         btnStart.setOnClickListener(new View.OnClickListener() {   
    34.   
    35.             @Override  
    36.             public void onClick(View v) {   
    37.   
    38.                 System.out.println("--开始记时---");   
    39.                 String ss = edtSetTime.getText().toString();   
    40.                 if (!(ss.equals("") && ss != null)) {   
    41.                     startTime = Integer.parseInt(edtSetTime.getText()   
    42.                             .toString());   
    43.                 }   
    44.                 // 设置开始讲时时间   
    45.                 chronometer.setBase(SystemClock.elapsedRealtime());   
    46.                 // 开始记时   
    47.                 chronometer.start();   
    48.   
    49.             }   
    50.         });   
    51.   
    52.         btnStop.setOnClickListener(new View.OnClickListener() {   
    53.             @Override  
    54.             public void onClick(View v) {   
    55.                 // 停止   
    56.                 chronometer.stop();   
    57.             }   
    58.   
    59.         });   
    60.   
    61.         // 重置   
    62.         btnRest.setOnClickListener(new View.OnClickListener() {   
    63.             @Override  
    64.             public void onClick(View v) {   
    65.                 chronometer.setBase(SystemClock.elapsedRealtime());   
    66.   
    67.             }   
    68.   
    69.         });   
    70.         chronometer   
    71.                 .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {   
    72.                     @Override  
    73.                     public void onChronometerTick(Chronometer chronometer) {   
    74.                         // 如果开始计时到现在超过了startime秒   
    75.                         if (SystemClock.elapsedRealtime()   
    76.                                 - chronometer.getBase() > startTime * 1000) {   
    77.                             chronometer.stop();   
    78.                             // 给用户提示   
    79.                             showDialog();   
    80.                         }   
    81.                     }   
    82.                 });   
    83.     }   
    84.   
    85.     protected void showDialog() {   
    86.         AlertDialog.Builder builder = new AlertDialog.Builder(this);   
    87.          builder.setIcon(R.drawable.eb28d25);   
    88.         builder.setTitle("警告").setMessage("时间到")   
    89.                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {   
    90.                     @Override  
    91.                     public void onClick(DialogInterface dialog, int which) {   
    92.                     }   
    93.                 });   
    94.   
    95.         AlertDialog dialog = builder.create();   
    96.         dialog.show();   
    97.     }   
    98. }  
  • 相关阅读:
    Watir: 当出现错误提示AutoItX3.dll 没有注册的时候,该怎么处理?
    Watir 能够为你做什么?
    第三章:JMeter 常用的测试元件——线程组
    第二章:JMeter 目录结构分析
    第二章:JMeter 工作原理
    第二章:JMeter 测试结果的名词解释
    第二章:JMeter 中常见 的术语
    第二章:JMeter 的介绍
    第一章:性能测试的常见指标
    第一章:性能测试的常见分类
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3937649.html
Copyright © 2011-2022 走看看