Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
本文為大家演示了如何使用Chronometer控件實(shí)現(xiàn)Android計(jì)時(shí)器的實(shí)例。
先貼上最終的實(shí)現(xiàn)效果圖:

Android計(jì)時(shí)器實(shí)現(xiàn)思路
使用Chronometer控件實(shí)現(xiàn)計(jì)器的操作。通過(guò)設(shè)置setBase(long base)來(lái)設(shè)置初始時(shí)間,然后為其添加一個(gè) setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件來(lái)判斷時(shí)間是否到了,然后再調(diào)用其stop()方法實(shí)現(xiàn)停止計(jì)時(shí)。
Android計(jì)時(shí)器實(shí)現(xiàn)代碼
main.xml:
<?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="設(shè)置時(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="開(kāi)始記時(shí)" />
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止記時(shí)" />
<Button
android:id="@+id/btnReset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置" />
</LinearLayout>
</LinearLayout>
Activity代碼:
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("--開(kāi)始記時(shí)---");
String ss = edtSetTime.getText().toString();
if (!(ss.equals("") && ss != null)) {
startTime = Integer.parseInt(edtSetTime.getText()
.toString());
}
// 設(shè)置開(kāi)始講時(shí)時(shí)間
chronometer.setBase(SystemClock.elapsedRealtime());
// 開(kāi)始記時(shí)
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) {
// 如果開(kāi)始計(jì)時(shí)到現(xiàn)在超過(guò)了startime秒
if (SystemClock.elapsedRealtime()
- chronometer.getBase() > startTime * 1000) {
chronometer.stop();
// 給用戶(hù)提示
showDialog();
}
}
});
}
protected void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.eb28d25);
builder.setTitle("警告").setMessage("時(shí)間到")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
以上就是關(guān)于Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器的相關(guān)函數(shù),希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android studio 運(yùn)行main 函數(shù)的方法
- 詳解Android應(yīng)用main函數(shù)的調(diào)用
- Android Studio生成函數(shù)注釋的實(shí)現(xiàn)方法
- Android自定義View的三個(gè)構(gòu)造函數(shù)
- Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法總結(jié)
- Android自定義view 你所需要知道的基本函數(shù)總結(jié)
- Android 自定義View的構(gòu)造函數(shù)詳細(xì)介紹
- Android編程之匿名內(nèi)部類(lèi)與回調(diào)函數(shù)用法分析
- Android自定義View構(gòu)造函數(shù)詳解
- Android nativePollOnce函數(shù)解析
相關(guān)文章
探討:android項(xiàng)目開(kāi)發(fā) 統(tǒng)籌兼顧 需要考慮的因素
本篇文章是對(duì)基于android項(xiàng)目開(kāi)發(fā) 統(tǒng)籌兼顧 需要考慮的因素進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android中編寫(xiě)屬性動(dòng)畫(huà)PropertyAnimation的進(jìn)階實(shí)例
這篇文章主要介紹了Android中編寫(xiě)屬性動(dòng)畫(huà)PropertyAnimation的進(jìn)階實(shí)例,包括一些縮放和淡入淡出效果的設(shè)計(jì),強(qiáng)大且不算復(fù)雜,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件
大家好,本篇文章主要講的是Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-02-02
Kotlin類(lèi)的繼承實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Kotlin類(lèi)的繼承,在Java中類(lèi)的繼承默認(rèn)是繼承父類(lèi)的方法和參數(shù)的,但是在kotlin中默認(rèn)是不繼承的,那么我們接下來(lái)來(lái)驗(yàn)證2022-09-09
Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法,需要的朋友可以參考下2021-09-09
Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)的兩種方式案例詳解
這篇文章主要介紹了Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)的兩種方式,需要的朋友可以參考下2021-08-08
Android查看電池電量的方法(基于BroadcastReceiver)
這篇文章主要介紹了Android查看電池電量的方法,結(jié)合實(shí)例分析了Android使用BroadcastReceiver實(shí)現(xiàn)針對(duì)電池電量的查詢(xún)技巧,需要的朋友可以參考下2016-01-01
Android斬首行動(dòng)接口預(yù)請(qǐng)求
這篇文章主要為大家介紹了Android斬首行動(dòng)之接口預(yù)請(qǐng)求實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

