展会信息港展会大全

Android开发:Chronometer控件实现计时器
来源:互联网   发布日期:2016-01-19 11:30:14   浏览:3089次  

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

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

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

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

Android计时器实现思路

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

Android计时器实现代码

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="设置时间:"/>

<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());

}

// 设置开始讲时时间

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) {

// 如果开始计时到现在超过了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();

}

}

赞助本站

人工智能实验室

相关热词: Chronometer 计时器

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港