Android開發(fā):Chronometer控件實現(xiàn)計時器
作者:佚名
使用Chronometer控件實現(xiàn)計器的操作。通過設(shè)置setBase(long base)來設(shè)置初始時間,然后為其添加一 個 setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件來判斷時間是否到了,然后再調(diào)用其stop()方法實現(xiàn)停止計時。
本文為大家演示了如何使用Chronometer控件實現(xiàn)Android計時器的實例。
先貼上最終的實現(xiàn)效果圖:
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代碼
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns: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="設(shè)置時間:"/>
- <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;
- publicclass ChronometerDemoActivity extends Activity {
- privateint startTime = 0;
- publicvoid 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
- publicvoid onClick(View v) {
- System.out.println("--開始記時---");
- String ss = edtSetTime.getText().toString();
- if (!(ss.equals("") && ss != null)) {
- startTime = Integer.parseInt(edtSetTime.getText()
- .toString());
- }
- // 設(shè)置開始講時時間
- chronometer.setBase(SystemClock.elapsedRealtime());
- // 開始記時
- chronometer.start();
- }
- });
- btnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // 停止
- chronometer.stop();
- }
- });
- // 重置
- btnRest.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- chronometer.setBase(SystemClock.elapsedRealtime());
- }
- });
- chronometer
- .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
- @Override
- publicvoid onChronometerTick(Chronometer chronometer) {
- // 如果開始計時到現(xiàn)在超過了startime秒
- if (SystemClock.elapsedRealtime()
- - chronometer.getBase() > startTime * 1000) {
- chronometer.stop();
- // 給用戶提示
- showDialog();
- }
- }
- });
- }
- protectedvoid showDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(R.drawable.eb28d25);
- builder.setTitle("警告").setMessage("時間到")
- .setPositiveButton("確定", new DialogInterface.OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- }
責(zé)任編輯:閆佳明
來源:
jizhuomi