Android控件Chronometer定時器的實(shí)現(xiàn)方法
Chronometer是一個簡單的定時器,你可以給它一個開始時間,并以此定時,或者如果你不給它一個開始時間,它將會使用你的時間通話開始。默認(rèn)情況下它會顯示在當(dāng)前定時器的值的形式“分:秒”或“H:MM:SS的”,或者可以使用的Set(字符串)格式的定時器值到一個任意字符串
1.重要屬性
android:format:定義時間的格式如:hh:mm:ss
2.重要方法
setBase(long base):設(shè)置倒計時定時器
setFormat(String format):設(shè)置顯示時間的格式。
start():開始計時
stop() :停止計時
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):當(dāng)計時器改變時調(diào)用。
3.實(shí)例
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Chronometer android:id="@+id/chronometer" android:format="Initial format: " android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="30dip" android:paddingTop="30dip" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始"> <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="重置"> </Button> <Button android:id="@+id/set_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設(shè)置格式"> </Button> <Button android:id="@+id/clear_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除格式"> </Button> </LinearLayout>
主程序:
package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class ChronometerDemo extends Activity {
private Chronometer mChronometer;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chronometerpage);
Button button;
mChronometer = (Chronometer) findViewById(R.id.chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
button = (Button) findViewById(R.id.set_format);
button.setOnClickListener(mSetFormatListener);
button = (Button) findViewById(R.id.clear_format);
button.setOnClickListener(mClearFormatListener);
}
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);
}
};
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android定時器Timer的停止和重啟實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)定時器的五種方法實(shí)例詳解
- Android 實(shí)現(xiàn)定時器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例
- Android 三種實(shí)現(xiàn)定時器詳解及實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)定時器的3種方法
- Android中使用定時器的三種方法
- Android三種實(shí)現(xiàn)定時器的方法
- 基于Android中實(shí)現(xiàn)定時器的3種解決方法
- 詳解Android實(shí)現(xiàn)定時器的幾種方法
相關(guān)文章
關(guān)于Android bitmap你不知道的一些事
這篇文章主要為大家詳細(xì)介紹了關(guān)于Android bitmap你不知道的一些事,使用bitmap需要注意的一些細(xì)節(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android?側(cè)滑按鈕的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android?側(cè)滑按鈕的實(shí)現(xiàn),本文結(jié)合示例代碼圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動刪除"-2
這篇文章主要介紹了Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動刪除"(二)功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
android自定義view實(shí)現(xiàn)圓周運(yùn)動
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)逆時針和順時針轉(zhuǎn)動的圓周運(yùn)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
Android App開發(fā)的自動化測試框架UI Automator使用教程
UI Automator為Android程序的UI開發(fā)提供了測試環(huán)境,這里我們就來看一下Android App開發(fā)的自動化測試框架UI Automator使用教程,需要的朋友可以參考下2016-07-07
一個強(qiáng)大的側(cè)滑菜單控件ASwipeLayout
這篇文章主要為大家詳細(xì)介紹了強(qiáng)大的側(cè)滑菜單控件ASwipeLayout使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android中關(guān)于FragmentA嵌套FragmentB的問題
這篇文章主要為大家詳細(xì)介紹了Android中關(guān)于FragmentA嵌套FragmentB的問題,感興趣的小伙伴們可以參考一下2016-08-08

