Android編程之計(jì)時(shí)器Chronometer簡(jiǎn)單示例
本文實(shí)例講述了Android計(jì)時(shí)器Chronometer。分享給大家供大家參考,具體如下:
Android中Chronometer控件繼承自TextView,這個(gè)組件可以用1秒的時(shí)間間隔進(jìn)行計(jì)時(shí),并顯示出計(jì)時(shí)結(jié)果。就是我們常說(shuō)的計(jì)時(shí)器工具。
public class ChronometerActivity extends Activity implements OnClickListener {
private Chronometer mChronometer;
private Button start, stop, reset, format, clear_format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chronometer);
// 初始化View widget
initViews();
// 設(shè)置監(jiān)聽(tīng)事件
initListeners();
}
private void initListeners() {
start.setOnClickListener(this);
stop.setOnClickListener(this);
reset.setOnClickListener(this);
format.setOnClickListener(this);
clear_format.setOnClickListener(this);
}
private void initViews() {
mChronometer = (Chronometer) findViewById(R.id.chronometer);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
reset = (Button) findViewById(R.id.reset);
format = (Button) findViewById(R.id.set_format);
clear_format = (Button) findViewById(R.id.clear_format);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
View.OnClickListener mSetFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat("Formatted time (%s)");
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
mChronometer.start();// 開(kāi)始計(jì)時(shí)
break;
case R.id.stop:
mChronometer.stop();// 暫停計(jì)時(shí)
break;
case R.id.reset:
mChronometer.setBase(SystemClock.elapsedRealtime());// 從開(kāi)機(jī)到現(xiàn)在的毫秒數(shù)
break;
case R.id.set_format:
// 需要一個(gè)String變量,并使用"%s"表示計(jì)時(shí)信息
mChronometer.setFormat("時(shí)間累計(jì):%s秒");
break;
case R.id.clear_format:
mChronometer.setFormat(null);
break;
}
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dip" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format="@string/chronometer_initial_format"
android:paddingBottom="30dip"
android:paddingTop="30dip"
android:textSize="15sp" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開(kāi)始" >
<requestFocus />
</Button>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暫停" >
</Button>
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重新計(jì)時(shí)" >
</Button>
<Button
android:id="@+id/set_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息說(shuō)明" >
</Button>
<Button
android:id="@+id/clear_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消說(shuō)明" >
</Button>
</LinearLayout>
PS:這里再為大家推薦幾款相關(guān)的在線工具供大家參考:
在線秒表工具:
http://tools.jb51.net/bianmin/miaobiao
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android日期與時(shí)間操作技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
- Android計(jì)時(shí)器chronometer使用實(shí)例講解
- 學(xué)習(xí)使用Android Chronometer計(jì)時(shí)器
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開(kāi)發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android 編程下的計(jì)時(shí)器代碼
- Android計(jì)時(shí)器控件Chronometer應(yīng)用實(shí)例
相關(guān)文章
Android SeekBar實(shí)現(xiàn)滑動(dòng)條效果
這篇文章主要為大家詳細(xì)介紹了Android SeekBar實(shí)現(xiàn)滑動(dòng)條效果,可以改變并顯示當(dāng)前進(jìn)度的拖動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android自定義View實(shí)現(xiàn)地鐵顯示牌效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)地鐵顯示牌效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android TreeView實(shí)現(xiàn)帶復(fù)選框樹(shù)形組織結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了Android TreeView實(shí)現(xiàn)帶復(fù)選框樹(shù)形組織結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android實(shí)現(xiàn)夜間模式切換功能實(shí)現(xiàn)代碼
現(xiàn)在很多App都有夜間模式,特別是閱讀類的App,夜間模式現(xiàn)在已經(jīng)是閱讀類App的標(biāo)配,本篇文章主要介紹了Android實(shí)現(xiàn)夜間模式功能實(shí)現(xiàn)代碼,有興趣的可以了解一下。2017-03-03
webview添加參數(shù)與修改請(qǐng)求頭的user-agent實(shí)例
這篇文章主要介紹了webview添加參數(shù)與修改請(qǐng)求頭的user-agent實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
android LabelView實(shí)現(xiàn)標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了android LabelView實(shí)現(xiàn)標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
android手機(jī)獲取唯一標(biāo)識(shí)的方法
這篇文章主要 為大家詳細(xì)介紹了android手機(jī)獲取唯一標(biāo)識(shí)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
DataBinding onClick的七種點(diǎn)擊方式
這篇文章主要給大家介紹了關(guān)于DataBinding onClick的七種點(diǎn)擊方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android實(shí)現(xiàn)房貸計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)房貸計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

