本文为大家演示了如何使用Chronometer控件实现Android计时器的实例。
先贴上最终的实现效果图:
Android计时器实现思路
使用Chronometer控件实现计器的操作。通过设置setBase(long base)来设置初始时间,然后为其添加一个 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件来判断时间是否到了,然后再调用其stop()方法实现停止计时。
Android计时器实现代码
main.xml:
XML/HTML代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/back"
- android:gravity="center"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="4"
- android:gravity="center"
- android:text="设置时间:" />
- <EditText
- android:id="@+id/edt_settime"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:inputType="number" />
- </LinearLayout>
- <Chronometer
- android:id="@+id/chronometer"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#ff0000"
- android:textSize="60dip" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/btnStart"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="开始记时" />
- <Button
- android:id="@+id/btnStop"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止记时" />
- <Button
- android:id="@+id/btnReset"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="重置" />
- </LinearLayout>
- </LinearLayout>
Activity代码:
Java代码
- package com.jiahui.chronometer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.text.format.Time;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Chronometer;
- import android.widget.EditText;
- public class ChronometerDemoActivity extends Activity {
- private int startTime = 0;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
- Button btnStart = (Button) findViewById(R.id.btnStart);
- Button btnStop = (Button) findViewById(R.id.btnStop);
- Button btnRest = (Button) findViewById(R.id.btnReset);
- final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);
- btnStart.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- System.out.println("--开始记时---");
- String ss = edtSetTime.getText().toString();
- if (!(ss.equals("") && ss != null)) {
- startTime = Integer.parseInt(edtSetTime.getText()
- .toString());
- }
- // 设置开始讲时时间
- chronometer.setBase(SystemClock.elapsedRealtime());
- // 开始记时
- chronometer.start();
- }
- });
- btnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 停止
- chronometer.stop();
- }
- });
- // 重置
- btnRest.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- chronometer.setBase(SystemClock.elapsedRealtime());
- }
- });
- chronometer
- .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
- @Override
- public void onChronometerTick(Chronometer chronometer) {
- // 如果开始计时到现在超过了startime秒
- if (SystemClock.elapsedRealtime()
- - chronometer.getBase() > startTime * 1000) {
- chronometer.stop();
- // 给用户提示
- showDialog();
- }
- }
- });
- }
- protected void showDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(R.drawable.eb28d25);
- builder.setTitle("警告").setMessage("时间到")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- }